--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.
--Data Schema For Version 0.0.0.0

--CREATE TABLE [Ancestor]
if not exists(select * from sysobjects where name = 'Ancestor' and xtype = 'U')
CREATE TABLE [dbo].[Ancestor] (
[AncestorId] [Int] IDENTITY (1, 1) NOT NULL ,
[Name] [VarChar] (50) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__ANCESTOR_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__ANCESTOR_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [Child]
if not exists(select * from sysobjects where name = 'Child' and xtype = 'U')
CREATE TABLE [dbo].[Child] (
[ChildId] [Int] IDENTITY (1, 1) NOT NULL ,
[FparentId] [Int] NOT NULL ,
[Name2] [VarChar] (100) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__CHILD_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__CHILD_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [Descendant]
if not exists(select * from sysobjects where name = 'Descendant' and xtype = 'U')
CREATE TABLE [dbo].[Descendant] (
[AncestorId] [Int] NOT NULL ,
[FdecendantParentId] [Int] NULL ,
[LastName] [VarChar] (50) NOT NULL  CONSTRAINT [DF__DESCENDANT_LASTNAME] DEFAULT ('Davis'),
[Price] [Decimal] (20, 0) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__DESCENDANT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__DESCENDANT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [DescendantParent]
if not exists(select * from sysobjects where name = 'DescendantParent' and xtype = 'U')
CREATE TABLE [dbo].[DescendantParent] (
[DecendantParentId] [Int] IDENTITY (1, 1) NOT NULL ,
[Qqq] [VarChar] (50) NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__DESCENDANTPARENT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__DESCENDANTPARENT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [DoubleKeyIdentity]
if not exists(select * from sysobjects where name = 'DoubleKeyIdentity' and xtype = 'U')
CREATE TABLE [dbo].[DoubleKeyIdentity] (
[Key] [Int] IDENTITY (1, 1) NOT NULL ,
[Name] [VarChar] (50) NULL ,
[Pk1] [VarChar] (50) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__DOUBLEKEYIDENTITY_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__DOUBLEKEYIDENTITY_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [DoubleLinkOneOneChild]
if not exists(select * from sysobjects where name = 'DoubleLinkOneOneChild' and xtype = 'U')
CREATE TABLE [dbo].[DoubleLinkOneOneChild] (
[DoubleLinkChildKey1] [Int] NOT NULL ,
[DoubleLinkChildKey2] [Int] NOT NULL ,
[DoubleLinkParentKey1] [Int] NOT NULL ,
[DoubleLinkParentKey2] [Int] NOT NULL ,
[Qqq] [VarChar] (50) NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONECHILD_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONECHILD_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [DoubleLinkOneOneParent]
if not exists(select * from sysobjects where name = 'DoubleLinkOneOneParent' and xtype = 'U')
CREATE TABLE [dbo].[DoubleLinkOneOneParent] (
[DoubleLinkParentKey1] [Int] NOT NULL ,
[DoubleLinkParentKey2] [Int] NOT NULL ,
[Name] [VarChar] (50) NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONEPARENT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONEPARENT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [GuidLinkChild]
if not exists(select * from sysobjects where name = 'GuidLinkChild' and xtype = 'U')
CREATE TABLE [dbo].[GuidLinkChild] (
[ChildId] [Int] IDENTITY (1, 1) NOT NULL ,
[ParentId] [UniqueIdentifier] NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__GUIDLINKCHILD_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__GUIDLINKCHILD_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [GuidLinkParent]
if not exists(select * from sysobjects where name = 'GuidLinkParent' and xtype = 'U')
CREATE TABLE [dbo].[GuidLinkParent] (
[Id] [UniqueIdentifier] NOT NULL ,
[Name] [VarChar] (50) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__GUIDLINKPARENT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__GUIDLINKPARENT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [Left]
if not exists(select * from sysobjects where name = 'Left' and xtype = 'U')
CREATE TABLE [dbo].[Left] (
[LeftId] [Int] IDENTITY (1, 1) NOT NULL ,
[Name] [VarChar] (50) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__LEFT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__LEFT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [LeftRight]
if not exists(select * from sysobjects where name = 'LeftRight' and xtype = 'U')
CREATE TABLE [dbo].[LeftRight] (
[FleftId] [Int] NOT NULL ,
[FrightId] [Int] NOT NULL ) ON [PRIMARY]

GO

--CREATE TABLE [ManyTypes]
if not exists(select * from sysobjects where name = 'ManyTypes' and xtype = 'U')
CREATE TABLE [dbo].[ManyTypes] (
[GuidId] [UniqueIdentifier] NOT NULL ,
[TestBigint] [BigInt] NOT NULL ,
[TestBinary] [Binary] (100) NOT NULL ,
[TestBit] [Bit] NOT NULL ,
[TestChar] [Char] (100) NOT NULL ,
[TestDate] [Date] NOT NULL ,
[TestDatetime] [DateTime] NOT NULL ,
[TestDatetime2] [DateTime2] NOT NULL ,
[TestDatetimeoffset] [DateTimeOffset] NOT NULL ,
[TestDecimal] [Decimal] (20, 2) NOT NULL ,
[TestFloat] [Float] NOT NULL ,
[TestImage] [Image] NOT NULL ,
[TestInt] [Int] NOT NULL  CONSTRAINT [DF__MANYTYPES_TESTINT] DEFAULT (278),
[TestMoney] [Money] NOT NULL ,
[TestNchar] [NChar] (100) NOT NULL ,
[TestNtext] [NText] NOT NULL ,
[TestNvarchar] [NVarChar] (100) NOT NULL ,
[TestReal] [Real] NOT NULL ,
[TestSmalldatetime] [SmallDateTime] NOT NULL ,
[TestSmallint] [SmallInt] NOT NULL ,
[TestSmallmoney] [SmallMoney] NOT NULL ,
[TestText] [Text] NOT NULL ,
[TestTime] [Time] NOT NULL ,
[TestTinyint] [TinyInt] NOT NULL ,
[TestUniqueidentifier] [UniqueIdentifier] NOT NULL ,
[TestVarbinary] [VarBinary] (100) NOT NULL ,
[TestVarchar] [VarChar] (100) NOT NULL ,
[TestXml] [Xml] NOT NULL ,
[Testcalculated] AS (12+4),
[Varcharmax] [VarChar] (max) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__MANYTYPES_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__MANYTYPES_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [OneToOneChild]
if not exists(select * from sysobjects where name = 'OneToOneChild' and xtype = 'U')
CREATE TABLE [dbo].[OneToOneChild] (
[LastName] [VarChar] (50) NULL ,
[UserId] [Int] NOT NULL ) ON [PRIMARY]

GO

--CREATE TABLE [OneToOneParent]
if not exists(select * from sysobjects where name = 'OneToOneParent' and xtype = 'U')
CREATE TABLE [dbo].[OneToOneParent] (
[FirstName] [VarChar] (50) NULL ,
[UserId] [Int] IDENTITY (1, 1) NOT NULL ) ON [PRIMARY]

GO

--CREATE TABLE [Parent]
if not exists(select * from sysobjects where name = 'Parent' and xtype = 'U')
CREATE TABLE [dbo].[Parent] (
[FparentTypeId] [Int] NOT NULL ,
[Name] [VarChar] (100) NOT NULL ,
[ParentId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__PARENT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__PARENT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [ParentType]
if not exists(select * from sysobjects where name = 'ParentType' and xtype = 'U')
CREATE TABLE [dbo].[ParentType] (
[Name] [VarChar] (50) NOT NULL ,
[ParentTypeId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__PARENTTYPE_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__PARENTTYPE_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [Right]
if not exists(select * from sysobjects where name = 'Right' and xtype = 'U')
CREATE TABLE [dbo].[Right] (
[Name] [VarChar] (50) NOT NULL ,
[RightId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__RIGHT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__RIGHT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [Simple]
if not exists(select * from sysobjects where name = 'Simple' and xtype = 'U')
CREATE TABLE [dbo].[Simple] (
[MyReadonly] [Int] NOT NULL  CONSTRAINT [DF__SIMPLE_MYREADONLY] DEFAULT (0),
[Name] [VarChar] (50) NULL ,
[NewInt] [Int] NOT NULL  CONSTRAINT [DF__SIMPLE_NEWINT] DEFAULT (0),
[SimpleId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__SIMPLE_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__SIMPLE_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE TABLE [TreeNode]
if not exists(select * from sysobjects where name = 'TreeNode' and xtype = 'U')
CREATE TABLE [dbo].[TreeNode] (
[FparentTreeNodeId] [Int] NULL ,
[Name] [VarChar] (50) NOT NULL ,
[TreeNodeId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__TREENODE_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__TREENODE_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

--CREATE AUDIT TABLE FOR [Ancestor]
if not exists(select * from sysobjects where name = '__AUDIT__Ancestor' and xtype = 'U')
CREATE TABLE [dbo].[__AUDIT__Ancestor] (
[__rowid] [INT] NOT NULL IDENTITY,
[__action] [INT] NOT NULL,
[__insertdate] [DateTime] CONSTRAINT [DF__Ancestor__AUDIT] DEFAULT getdate() NOT NULL,
[ModifiedBy] [Varchar] (50) NULL,
[AncestorId] [Int] NULL,
[Name] [VarChar] (50) NULL
) ON [PRIMARY]

GO

--ENSURE ALL COLUMNS ARE CORRECT TYPE
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'AncestorId' and o.name = '__AUDIT__Ancestor')
ALTER TABLE [dbo].[__AUDIT__Ancestor] ADD [AncestorId] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__Ancestor] ALTER COLUMN [AncestorId] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Name' and o.name = '__AUDIT__Ancestor')
ALTER TABLE [dbo].[__AUDIT__Ancestor] ADD [Name] [VarChar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__Ancestor] ALTER COLUMN [Name] [VarChar] (50) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = '__AUDIT__Ancestor')
ALTER TABLE [dbo].[__AUDIT__Ancestor] ADD [ModifiedBy] [Varchar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__Ancestor] ALTER COLUMN [ModifiedBy] [Varchar] (50) NULL
GO

--CREATE AUDIT TABLE FOR [Descendant]
if not exists(select * from sysobjects where name = '__AUDIT__Descendant' and xtype = 'U')
CREATE TABLE [dbo].[__AUDIT__Descendant] (
[__rowid] [INT] NOT NULL IDENTITY,
[__action] [INT] NOT NULL,
[__insertdate] [DateTime] CONSTRAINT [DF__Descendant__AUDIT] DEFAULT getdate() NOT NULL,
[ModifiedBy] [Varchar] (50) NULL,
[AncestorId] [Int] NULL,
[FdecendantParentId] [Int] NULL,
[LastName] [VarChar] (50) NULL,
[Price] [Decimal] (20, 0) NULL
) ON [PRIMARY]

GO

--ENSURE ALL COLUMNS ARE CORRECT TYPE
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'AncestorId' and o.name = '__AUDIT__Descendant')
ALTER TABLE [dbo].[__AUDIT__Descendant] ADD [AncestorId] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__Descendant] ALTER COLUMN [AncestorId] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'FdecendantParentId' and o.name = '__AUDIT__Descendant')
ALTER TABLE [dbo].[__AUDIT__Descendant] ADD [FdecendantParentId] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__Descendant] ALTER COLUMN [FdecendantParentId] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'LastName' and o.name = '__AUDIT__Descendant')
ALTER TABLE [dbo].[__AUDIT__Descendant] ADD [LastName] [VarChar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__Descendant] ALTER COLUMN [LastName] [VarChar] (50) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Price' and o.name = '__AUDIT__Descendant')
ALTER TABLE [dbo].[__AUDIT__Descendant] ADD [Price] [Decimal] (20, 0) NULL
GO
ALTER TABLE [dbo].[__AUDIT__Descendant] ALTER COLUMN [Price] [Decimal] (20, 0) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = '__AUDIT__Descendant')
ALTER TABLE [dbo].[__AUDIT__Descendant] ADD [ModifiedBy] [Varchar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__Descendant] ALTER COLUMN [ModifiedBy] [Varchar] (50) NULL
GO

--CREATE AUDIT TABLE FOR [ManyTypes]
if not exists(select * from sysobjects where name = '__AUDIT__ManyTypes' and xtype = 'U')
CREATE TABLE [dbo].[__AUDIT__ManyTypes] (
[__rowid] [INT] NOT NULL IDENTITY,
[__action] [INT] NOT NULL,
[__insertdate] [DateTime] CONSTRAINT [DF__ManyTypes__AUDIT] DEFAULT getdate() NOT NULL,
[ModifiedBy] [Varchar] (50) NULL,
[GuidId] [UniqueIdentifier] NULL,
[TestBigint] [BigInt] NULL,
[TestBinary] [Binary] (100) NULL,
[TestBit] [Bit] NULL,
[Testcalculated] [Decimal] (18, 4) NULL,
[TestChar] [Char] (100) NULL,
[TestDate] [Date] NULL,
[TestDatetime] [DateTime] NULL,
[TestDatetime2] [DateTime2] NULL,
[TestDatetimeoffset] [DateTimeOffset] NULL,
[TestDecimal] [Decimal] (20, 2) NULL,
[TestFloat] [Float] NULL,
[TestInt] [Int] NULL,
[TestMoney] [Money] NULL,
[TestNchar] [NChar] (100) NULL,
[TestNvarchar] [NVarChar] (100) NULL,
[TestReal] [Real] NULL,
[TestSmalldatetime] [SmallDateTime] NULL,
[TestSmallint] [SmallInt] NULL,
[TestSmallmoney] [SmallMoney] NULL,
[TestTime] [Time] NULL,
[TestTinyint] [TinyInt] NULL,
[TestUniqueidentifier] [UniqueIdentifier] NULL,
[TestVarbinary] [VarBinary] (100) NULL,
[TestVarchar] [VarChar] (100) NULL,
[TestXml] [Xml] NULL,
[Varcharmax] [VarChar] (max) NULL
) ON [PRIMARY]

GO

--ENSURE ALL COLUMNS ARE CORRECT TYPE
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'GuidId' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [GuidId] [UniqueIdentifier] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [GuidId] [UniqueIdentifier] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestBigint' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestBigint] [BigInt] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestBigint] [BigInt] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestBinary' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestBinary] [Binary] (100) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestBinary] [Binary] (100) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestBit' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestBit] [Bit] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestBit] [Bit] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Testcalculated' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [Testcalculated] [Decimal] (18, 4) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [Testcalculated] [Decimal] (18, 4) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestChar' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestChar] [Char] (100) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestChar] [Char] (100) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestDate' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestDate] [Date] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestDate] [Date] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestDatetime' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestDatetime] [DateTime] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestDatetime] [DateTime] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestDatetime2' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestDatetime2] [DateTime2] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestDatetime2] [DateTime2] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestDatetimeoffset' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestDatetimeoffset] [DateTimeOffset] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestDatetimeoffset] [DateTimeOffset] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestDecimal' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestDecimal] [Decimal] (20, 2) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestDecimal] [Decimal] (20, 2) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestFloat' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestFloat] [Float] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestFloat] [Float] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestInt' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestInt] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestInt] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestMoney' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestMoney] [Money] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestMoney] [Money] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestNchar' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestNchar] [NChar] (100) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestNchar] [NChar] (100) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestNvarchar' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestNvarchar] [NVarChar] (100) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestNvarchar] [NVarChar] (100) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestReal' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestReal] [Real] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestReal] [Real] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestSmalldatetime' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestSmalldatetime] [SmallDateTime] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestSmalldatetime] [SmallDateTime] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestSmallint' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestSmallint] [SmallInt] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestSmallint] [SmallInt] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestSmallmoney' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestSmallmoney] [SmallMoney] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestSmallmoney] [SmallMoney] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestTime' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestTime] [Time] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestTime] [Time] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestTinyint' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestTinyint] [TinyInt] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestTinyint] [TinyInt] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestUniqueidentifier' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestUniqueidentifier] [UniqueIdentifier] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestUniqueidentifier] [UniqueIdentifier] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestVarbinary' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestVarbinary] [VarBinary] (100) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestVarbinary] [VarBinary] (100) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestVarchar' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestVarchar] [VarChar] (100) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestVarchar] [VarChar] (100) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TestXml' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [TestXml] [Xml] NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [TestXml] [Xml] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Varcharmax' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [Varcharmax] [VarChar] (max) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [Varcharmax] [VarChar] (max) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = '__AUDIT__ManyTypes')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ADD [ModifiedBy] [Varchar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__ManyTypes] ALTER COLUMN [ModifiedBy] [Varchar] (50) NULL
GO

--CREATE AUDIT TABLE FOR [Simple]
if not exists(select * from sysobjects where name = '__AUDIT__Simple' and xtype = 'U')
CREATE TABLE [dbo].[__AUDIT__Simple] (
[__rowid] [INT] NOT NULL IDENTITY,
[__action] [INT] NOT NULL,
[__insertdate] [DateTime] CONSTRAINT [DF__Simple__AUDIT] DEFAULT getdate() NOT NULL,
[ModifiedBy] [Varchar] (50) NULL,
[MyReadonly] [Int] NULL,
[Name] [VarChar] (50) NULL,
[NewInt] [Int] NULL,
[SimpleId] [Int] NULL
) ON [PRIMARY]

GO

--ENSURE ALL COLUMNS ARE CORRECT TYPE
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'MyReadonly' and o.name = '__AUDIT__Simple')
ALTER TABLE [dbo].[__AUDIT__Simple] ADD [MyReadonly] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__Simple] ALTER COLUMN [MyReadonly] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Name' and o.name = '__AUDIT__Simple')
ALTER TABLE [dbo].[__AUDIT__Simple] ADD [Name] [VarChar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__Simple] ALTER COLUMN [Name] [VarChar] (50) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'NewInt' and o.name = '__AUDIT__Simple')
ALTER TABLE [dbo].[__AUDIT__Simple] ADD [NewInt] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__Simple] ALTER COLUMN [NewInt] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'SimpleId' and o.name = '__AUDIT__Simple')
ALTER TABLE [dbo].[__AUDIT__Simple] ADD [SimpleId] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__Simple] ALTER COLUMN [SimpleId] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = '__AUDIT__Simple')
ALTER TABLE [dbo].[__AUDIT__Simple] ADD [ModifiedBy] [Varchar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__Simple] ALTER COLUMN [ModifiedBy] [Varchar] (50) NULL
GO

--CREATE AUDIT TABLE FOR [TreeNode]
if not exists(select * from sysobjects where name = '__AUDIT__TreeNode' and xtype = 'U')
CREATE TABLE [dbo].[__AUDIT__TreeNode] (
[__rowid] [INT] NOT NULL IDENTITY,
[__action] [INT] NOT NULL,
[__insertdate] [DateTime] CONSTRAINT [DF__TreeNode__AUDIT] DEFAULT getdate() NOT NULL,
[ModifiedBy] [Varchar] (50) NULL,
[FparentTreeNodeId] [Int] NULL,
[Name] [VarChar] (50) NULL,
[TreeNodeId] [Int] NULL
) ON [PRIMARY]

GO

--ENSURE ALL COLUMNS ARE CORRECT TYPE
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'FparentTreeNodeId' and o.name = '__AUDIT__TreeNode')
ALTER TABLE [dbo].[__AUDIT__TreeNode] ADD [FparentTreeNodeId] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__TreeNode] ALTER COLUMN [FparentTreeNodeId] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Name' and o.name = '__AUDIT__TreeNode')
ALTER TABLE [dbo].[__AUDIT__TreeNode] ADD [Name] [VarChar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__TreeNode] ALTER COLUMN [Name] [VarChar] (50) NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TreeNodeId' and o.name = '__AUDIT__TreeNode')
ALTER TABLE [dbo].[__AUDIT__TreeNode] ADD [TreeNodeId] [Int] NULL
GO
ALTER TABLE [dbo].[__AUDIT__TreeNode] ALTER COLUMN [TreeNodeId] [Int] NULL
GO

if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = '__AUDIT__TreeNode')
ALTER TABLE [dbo].[__AUDIT__TreeNode] ADD [ModifiedBy] [Varchar] (50) NULL
GO
ALTER TABLE [dbo].[__AUDIT__TreeNode] ALTER COLUMN [ModifiedBy] [Varchar] (50) NULL
GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Ancestor]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Ancestor')
ALTER TABLE [dbo].[Ancestor] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Ancestor')
ALTER TABLE [dbo].[Ancestor] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__ANCESTOR_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Ancestor]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Ancestor')
ALTER TABLE [dbo].[Ancestor] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Ancestor')
ALTER TABLE [dbo].[Ancestor] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__ANCESTOR_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Ancestor]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Ancestor')
ALTER TABLE [dbo].[Ancestor] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Child]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Child')
ALTER TABLE [dbo].[Child] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Child')
ALTER TABLE [dbo].[Child] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__CHILD_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Child]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Child')
ALTER TABLE [dbo].[Child] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Child')
ALTER TABLE [dbo].[Child] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__CHILD_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Child]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Child')
ALTER TABLE [dbo].[Child] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Descendant]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Descendant')
ALTER TABLE [dbo].[Descendant] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Descendant')
ALTER TABLE [dbo].[Descendant] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__DESCENDANT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Descendant]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Descendant')
ALTER TABLE [dbo].[Descendant] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Descendant')
ALTER TABLE [dbo].[Descendant] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__DESCENDANT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Descendant]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Descendant')
ALTER TABLE [dbo].[Descendant] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [DescendantParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'DescendantParent')
ALTER TABLE [dbo].[DescendantParent] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'DescendantParent')
ALTER TABLE [dbo].[DescendantParent] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__DESCENDANTPARENT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [DescendantParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'DescendantParent')
ALTER TABLE [dbo].[DescendantParent] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'DescendantParent')
ALTER TABLE [dbo].[DescendantParent] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__DESCENDANTPARENT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [DescendantParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'DescendantParent')
ALTER TABLE [dbo].[DescendantParent] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [DoubleKeyIdentity]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'DoubleKeyIdentity')
ALTER TABLE [dbo].[DoubleKeyIdentity] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'DoubleKeyIdentity')
ALTER TABLE [dbo].[DoubleKeyIdentity] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__DOUBLEKEYIDENTITY_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [DoubleKeyIdentity]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'DoubleKeyIdentity')
ALTER TABLE [dbo].[DoubleKeyIdentity] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'DoubleKeyIdentity')
ALTER TABLE [dbo].[DoubleKeyIdentity] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__DOUBLEKEYIDENTITY_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [DoubleKeyIdentity]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'DoubleKeyIdentity')
ALTER TABLE [dbo].[DoubleKeyIdentity] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [DoubleLinkOneOneChild]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'DoubleLinkOneOneChild')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'DoubleLinkOneOneChild')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONECHILD_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [DoubleLinkOneOneChild]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'DoubleLinkOneOneChild')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'DoubleLinkOneOneChild')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONECHILD_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [DoubleLinkOneOneChild]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'DoubleLinkOneOneChild')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [DoubleLinkOneOneParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'DoubleLinkOneOneParent')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'DoubleLinkOneOneParent')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONEPARENT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [DoubleLinkOneOneParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'DoubleLinkOneOneParent')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'DoubleLinkOneOneParent')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__DOUBLELINKONEONEPARENT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [DoubleLinkOneOneParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'DoubleLinkOneOneParent')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [GuidLinkChild]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'GuidLinkChild')
ALTER TABLE [dbo].[GuidLinkChild] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'GuidLinkChild')
ALTER TABLE [dbo].[GuidLinkChild] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__GUIDLINKCHILD_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [GuidLinkChild]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'GuidLinkChild')
ALTER TABLE [dbo].[GuidLinkChild] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'GuidLinkChild')
ALTER TABLE [dbo].[GuidLinkChild] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__GUIDLINKCHILD_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [GuidLinkChild]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'GuidLinkChild')
ALTER TABLE [dbo].[GuidLinkChild] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [GuidLinkParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'GuidLinkParent')
ALTER TABLE [dbo].[GuidLinkParent] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'GuidLinkParent')
ALTER TABLE [dbo].[GuidLinkParent] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__GUIDLINKPARENT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [GuidLinkParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'GuidLinkParent')
ALTER TABLE [dbo].[GuidLinkParent] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'GuidLinkParent')
ALTER TABLE [dbo].[GuidLinkParent] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__GUIDLINKPARENT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [GuidLinkParent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'GuidLinkParent')
ALTER TABLE [dbo].[GuidLinkParent] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Left]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Left')
ALTER TABLE [dbo].[Left] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Left')
ALTER TABLE [dbo].[Left] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__LEFT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Left]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Left')
ALTER TABLE [dbo].[Left] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Left')
ALTER TABLE [dbo].[Left] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__LEFT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Left]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Left')
ALTER TABLE [dbo].[Left] ADD [Timestamp] [timestamp] NOT NULL

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [LeftRight]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'LeftRight')
ALTER TABLE [dbo].[LeftRight] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__LEFTRIGHT_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[LeftRight] DROP CONSTRAINT [DF__LEFTRIGHT_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'LeftRight')
ALTER TABLE [dbo].[LeftRight] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [LeftRight]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'LeftRight')
ALTER TABLE [dbo].[LeftRight] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__LEFTRIGHT_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[LeftRight] DROP CONSTRAINT [DF__LEFTRIGHT_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'LeftRight')
ALTER TABLE [dbo].[LeftRight] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [LeftRight]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'LeftRight')
ALTER TABLE [dbo].[LeftRight] DROP COLUMN [Timestamp]

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [ManyTypes]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'ManyTypes')
ALTER TABLE [dbo].[ManyTypes] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'ManyTypes')
ALTER TABLE [dbo].[ManyTypes] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__MANYTYPES_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [ManyTypes]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'ManyTypes')
ALTER TABLE [dbo].[ManyTypes] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'ManyTypes')
ALTER TABLE [dbo].[ManyTypes] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__MANYTYPES_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [ManyTypes]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'ManyTypes')
ALTER TABLE [dbo].[ManyTypes] ADD [Timestamp] [timestamp] NOT NULL

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [OneToOneChild]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'OneToOneChild')
ALTER TABLE [dbo].[OneToOneChild] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__ONETOONECHILD_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[OneToOneChild] DROP CONSTRAINT [DF__ONETOONECHILD_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'OneToOneChild')
ALTER TABLE [dbo].[OneToOneChild] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [OneToOneChild]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'OneToOneChild')
ALTER TABLE [dbo].[OneToOneChild] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__ONETOONECHILD_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[OneToOneChild] DROP CONSTRAINT [DF__ONETOONECHILD_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'OneToOneChild')
ALTER TABLE [dbo].[OneToOneChild] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [OneToOneChild]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'OneToOneChild')
ALTER TABLE [dbo].[OneToOneChild] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [OneToOneParent]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'OneToOneParent')
ALTER TABLE [dbo].[OneToOneParent] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__ONETOONEPARENT_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[OneToOneParent] DROP CONSTRAINT [DF__ONETOONEPARENT_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'OneToOneParent')
ALTER TABLE [dbo].[OneToOneParent] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [OneToOneParent]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'OneToOneParent')
ALTER TABLE [dbo].[OneToOneParent] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__ONETOONEPARENT_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[OneToOneParent] DROP CONSTRAINT [DF__ONETOONEPARENT_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'OneToOneParent')
ALTER TABLE [dbo].[OneToOneParent] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [OneToOneParent]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'OneToOneParent')
ALTER TABLE [dbo].[OneToOneParent] DROP COLUMN [Timestamp]

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Parent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Parent')
ALTER TABLE [dbo].[Parent] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Parent')
ALTER TABLE [dbo].[Parent] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__PARENT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Parent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Parent')
ALTER TABLE [dbo].[Parent] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Parent')
ALTER TABLE [dbo].[Parent] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__PARENT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Parent]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Parent')
ALTER TABLE [dbo].[Parent] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [ParentType]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'ParentType')
ALTER TABLE [dbo].[ParentType] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'ParentType')
ALTER TABLE [dbo].[ParentType] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__PARENTTYPE_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [ParentType]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'ParentType')
ALTER TABLE [dbo].[ParentType] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'ParentType')
ALTER TABLE [dbo].[ParentType] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__PARENTTYPE_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [ParentType]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'ParentType')
ALTER TABLE [dbo].[ParentType] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Right]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Right')
ALTER TABLE [dbo].[Right] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Right')
ALTER TABLE [dbo].[Right] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__RIGHT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Right]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Right')
ALTER TABLE [dbo].[Right] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Right')
ALTER TABLE [dbo].[Right] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__RIGHT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Right]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Right')
ALTER TABLE [dbo].[Right] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Simple]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Simple')
ALTER TABLE [dbo].[Simple] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Simple')
ALTER TABLE [dbo].[Simple] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__SIMPLE_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Simple]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Simple')
ALTER TABLE [dbo].[Simple] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Simple')
ALTER TABLE [dbo].[Simple] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__SIMPLE_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Simple]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Simple')
ALTER TABLE [dbo].[Simple] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [TreeNode]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'TreeNode')
ALTER TABLE [dbo].[TreeNode] ADD [CreatedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'TreeNode')
ALTER TABLE [dbo].[TreeNode] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__TREENODE_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [TreeNode]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'TreeNode')
ALTER TABLE [dbo].[TreeNode] ADD [ModifiedBy] [Varchar] (50) NULL
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'TreeNode')
ALTER TABLE [dbo].[TreeNode] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__TREENODE_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [TreeNode]
if not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'TreeNode')
ALTER TABLE [dbo].[TreeNode] ADD [Timestamp] [timestamp] NOT NULL

GO

--RENAME EXISTING PRIMARY KEYS IF NECESSARY
DECLARE @pkfixAncestor varchar(500)
SET @pkfixAncestor = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Ancestor')
if @pkfixAncestor <> '' and (BINARY_CHECKSUM(@pkfixAncestor) <> BINARY_CHECKSUM('PK_ANCESTOR')) exec('sp_rename '''+@pkfixAncestor+''', ''PK_ANCESTOR''')
DECLARE @pkfixChild varchar(500)
SET @pkfixChild = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Child')
if @pkfixChild <> '' and (BINARY_CHECKSUM(@pkfixChild) <> BINARY_CHECKSUM('PK_CHILD')) exec('sp_rename '''+@pkfixChild+''', ''PK_CHILD''')
DECLARE @pkfixDescendant varchar(500)
SET @pkfixDescendant = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Descendant')
if @pkfixDescendant <> '' and (BINARY_CHECKSUM(@pkfixDescendant) <> BINARY_CHECKSUM('PK_DESCENDANT')) exec('sp_rename '''+@pkfixDescendant+''', ''PK_DESCENDANT''')
DECLARE @pkfixDescendantParent varchar(500)
SET @pkfixDescendantParent = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'DescendantParent')
if @pkfixDescendantParent <> '' and (BINARY_CHECKSUM(@pkfixDescendantParent) <> BINARY_CHECKSUM('PK_DESCENDANTPARENT')) exec('sp_rename '''+@pkfixDescendantParent+''', ''PK_DESCENDANTPARENT''')
DECLARE @pkfixDoubleKeyIdentity varchar(500)
SET @pkfixDoubleKeyIdentity = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'DoubleKeyIdentity')
if @pkfixDoubleKeyIdentity <> '' and (BINARY_CHECKSUM(@pkfixDoubleKeyIdentity) <> BINARY_CHECKSUM('PK_DOUBLEKEYIDENTITY')) exec('sp_rename '''+@pkfixDoubleKeyIdentity+''', ''PK_DOUBLEKEYIDENTITY''')
DECLARE @pkfixDoubleLinkOneOneChild varchar(500)
SET @pkfixDoubleLinkOneOneChild = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'DoubleLinkOneOneChild')
if @pkfixDoubleLinkOneOneChild <> '' and (BINARY_CHECKSUM(@pkfixDoubleLinkOneOneChild) <> BINARY_CHECKSUM('PK_DOUBLELINKONEONECHILD')) exec('sp_rename '''+@pkfixDoubleLinkOneOneChild+''', ''PK_DOUBLELINKONEONECHILD''')
DECLARE @pkfixDoubleLinkOneOneParent varchar(500)
SET @pkfixDoubleLinkOneOneParent = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'DoubleLinkOneOneParent')
if @pkfixDoubleLinkOneOneParent <> '' and (BINARY_CHECKSUM(@pkfixDoubleLinkOneOneParent) <> BINARY_CHECKSUM('PK_DOUBLELINKONEONEPARENT')) exec('sp_rename '''+@pkfixDoubleLinkOneOneParent+''', ''PK_DOUBLELINKONEONEPARENT''')
DECLARE @pkfixGuidLinkChild varchar(500)
SET @pkfixGuidLinkChild = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'GuidLinkChild')
if @pkfixGuidLinkChild <> '' and (BINARY_CHECKSUM(@pkfixGuidLinkChild) <> BINARY_CHECKSUM('PK_GUIDLINKCHILD')) exec('sp_rename '''+@pkfixGuidLinkChild+''', ''PK_GUIDLINKCHILD''')
DECLARE @pkfixGuidLinkParent varchar(500)
SET @pkfixGuidLinkParent = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'GuidLinkParent')
if @pkfixGuidLinkParent <> '' and (BINARY_CHECKSUM(@pkfixGuidLinkParent) <> BINARY_CHECKSUM('PK_GUIDLINKPARENT')) exec('sp_rename '''+@pkfixGuidLinkParent+''', ''PK_GUIDLINKPARENT''')
DECLARE @pkfixLeft varchar(500)
SET @pkfixLeft = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Left')
if @pkfixLeft <> '' and (BINARY_CHECKSUM(@pkfixLeft) <> BINARY_CHECKSUM('PK_LEFT')) exec('sp_rename '''+@pkfixLeft+''', ''PK_LEFT''')
DECLARE @pkfixLeftRight varchar(500)
SET @pkfixLeftRight = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'LeftRight')
if @pkfixLeftRight <> '' and (BINARY_CHECKSUM(@pkfixLeftRight) <> BINARY_CHECKSUM('PK_LEFTRIGHT')) exec('sp_rename '''+@pkfixLeftRight+''', ''PK_LEFTRIGHT''')
DECLARE @pkfixManyTypes varchar(500)
SET @pkfixManyTypes = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'ManyTypes')
if @pkfixManyTypes <> '' and (BINARY_CHECKSUM(@pkfixManyTypes) <> BINARY_CHECKSUM('PK_MANYTYPES')) exec('sp_rename '''+@pkfixManyTypes+''', ''PK_MANYTYPES''')
DECLARE @pkfixOneToOneChild varchar(500)
SET @pkfixOneToOneChild = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'OneToOneChild')
if @pkfixOneToOneChild <> '' and (BINARY_CHECKSUM(@pkfixOneToOneChild) <> BINARY_CHECKSUM('PK_ONETOONECHILD')) exec('sp_rename '''+@pkfixOneToOneChild+''', ''PK_ONETOONECHILD''')
DECLARE @pkfixOneToOneParent varchar(500)
SET @pkfixOneToOneParent = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'OneToOneParent')
if @pkfixOneToOneParent <> '' and (BINARY_CHECKSUM(@pkfixOneToOneParent) <> BINARY_CHECKSUM('PK_ONETOONEPARENT')) exec('sp_rename '''+@pkfixOneToOneParent+''', ''PK_ONETOONEPARENT''')
DECLARE @pkfixParent varchar(500)
SET @pkfixParent = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Parent')
if @pkfixParent <> '' and (BINARY_CHECKSUM(@pkfixParent) <> BINARY_CHECKSUM('PK_PARENT')) exec('sp_rename '''+@pkfixParent+''', ''PK_PARENT''')
DECLARE @pkfixParentType varchar(500)
SET @pkfixParentType = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'ParentType')
if @pkfixParentType <> '' and (BINARY_CHECKSUM(@pkfixParentType) <> BINARY_CHECKSUM('PK_PARENTTYPE')) exec('sp_rename '''+@pkfixParentType+''', ''PK_PARENTTYPE''')
DECLARE @pkfixRight varchar(500)
SET @pkfixRight = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Right')
if @pkfixRight <> '' and (BINARY_CHECKSUM(@pkfixRight) <> BINARY_CHECKSUM('PK_RIGHT')) exec('sp_rename '''+@pkfixRight+''', ''PK_RIGHT''')
DECLARE @pkfixSimple varchar(500)
SET @pkfixSimple = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Simple')
if @pkfixSimple <> '' and (BINARY_CHECKSUM(@pkfixSimple) <> BINARY_CHECKSUM('PK_SIMPLE')) exec('sp_rename '''+@pkfixSimple+''', ''PK_SIMPLE''')
DECLARE @pkfixTreeNode varchar(500)
SET @pkfixTreeNode = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'TreeNode')
if @pkfixTreeNode <> '' and (BINARY_CHECKSUM(@pkfixTreeNode) <> BINARY_CHECKSUM('PK_TREENODE')) exec('sp_rename '''+@pkfixTreeNode+''', ''PK_TREENODE''')
GO

--PRIMARY KEY FOR TABLE [Ancestor]
if not exists(select * from sysobjects where name = 'PK_ANCESTOR' and xtype = 'PK')
ALTER TABLE [dbo].[Ancestor] WITH NOCHECK ADD 
CONSTRAINT [PK_ANCESTOR] PRIMARY KEY CLUSTERED
(
	[AncestorId]
) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [__AUDIT__Ancestor]
if not exists(select * from sysobjects where name = 'PK___AUDIT__ANCESTOR' and xtype = 'PK')
ALTER TABLE [dbo].[__AUDIT__Ancestor] WITH NOCHECK ADD
CONSTRAINT [PK___AUDIT__ANCESTOR] PRIMARY KEY CLUSTERED ([__rowid]) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [Child]
if not exists(select * from sysobjects where name = 'PK_CHILD' and xtype = 'PK')
ALTER TABLE [dbo].[Child] WITH NOCHECK ADD 
CONSTRAINT [PK_CHILD] PRIMARY KEY CLUSTERED
(
	[ChildId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__Child]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__CHILD'))
ALTER TABLE [dbo].[__AUDIT__Child] DROP CONSTRAINT [PK___AUDIT__CHILD]
GO

--PRIMARY KEY FOR TABLE [Descendant]
if not exists(select * from sysobjects where name = 'PK_DESCENDANT' and xtype = 'PK')
ALTER TABLE [dbo].[Descendant] WITH NOCHECK ADD 
CONSTRAINT [PK_DESCENDANT] PRIMARY KEY CLUSTERED
(
	[AncestorId]
) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [__AUDIT__Descendant]
if not exists(select * from sysobjects where name = 'PK___AUDIT__DESCENDANT' and xtype = 'PK')
ALTER TABLE [dbo].[__AUDIT__Descendant] WITH NOCHECK ADD
CONSTRAINT [PK___AUDIT__DESCENDANT] PRIMARY KEY CLUSTERED ([__rowid]) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [DescendantParent]
if not exists(select * from sysobjects where name = 'PK_DESCENDANTPARENT' and xtype = 'PK')
ALTER TABLE [dbo].[DescendantParent] WITH NOCHECK ADD 
CONSTRAINT [PK_DESCENDANTPARENT] PRIMARY KEY CLUSTERED
(
	[DecendantParentId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__DescendantParent]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__DESCENDANTPARENT'))
ALTER TABLE [dbo].[__AUDIT__DescendantParent] DROP CONSTRAINT [PK___AUDIT__DESCENDANTPARENT]
GO

--PRIMARY KEY FOR TABLE [DoubleKeyIdentity]
if not exists(select * from sysobjects where name = 'PK_DOUBLEKEYIDENTITY' and xtype = 'PK')
ALTER TABLE [dbo].[DoubleKeyIdentity] WITH NOCHECK ADD 
CONSTRAINT [PK_DOUBLEKEYIDENTITY] PRIMARY KEY CLUSTERED
(
	[Key],
	[Pk1]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__DoubleKeyIdentity]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__DOUBLEKEYIDENTITY'))
ALTER TABLE [dbo].[__AUDIT__DoubleKeyIdentity] DROP CONSTRAINT [PK___AUDIT__DOUBLEKEYIDENTITY]
GO

--PRIMARY KEY FOR TABLE [DoubleLinkOneOneChild]
if not exists(select * from sysobjects where name = 'PK_DOUBLELINKONEONECHILD' and xtype = 'PK')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] WITH NOCHECK ADD 
CONSTRAINT [PK_DOUBLELINKONEONECHILD] PRIMARY KEY CLUSTERED
(
	[DoubleLinkChildKey1],
	[DoubleLinkChildKey2]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__DoubleLinkOneOneChild]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__DOUBLELINKONEONECHILD'))
ALTER TABLE [dbo].[__AUDIT__DoubleLinkOneOneChild] DROP CONSTRAINT [PK___AUDIT__DOUBLELINKONEONECHILD]
GO

--PRIMARY KEY FOR TABLE [DoubleLinkOneOneParent]
if not exists(select * from sysobjects where name = 'PK_DOUBLELINKONEONEPARENT' and xtype = 'PK')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] WITH NOCHECK ADD 
CONSTRAINT [PK_DOUBLELINKONEONEPARENT] PRIMARY KEY CLUSTERED
(
	[DoubleLinkParentKey1],
	[DoubleLinkParentKey2]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__DoubleLinkOneOneParent]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__DOUBLELINKONEONEPARENT'))
ALTER TABLE [dbo].[__AUDIT__DoubleLinkOneOneParent] DROP CONSTRAINT [PK___AUDIT__DOUBLELINKONEONEPARENT]
GO

--PRIMARY KEY FOR TABLE [GuidLinkChild]
if not exists(select * from sysobjects where name = 'PK_GUIDLINKCHILD' and xtype = 'PK')
ALTER TABLE [dbo].[GuidLinkChild] WITH NOCHECK ADD 
CONSTRAINT [PK_GUIDLINKCHILD] PRIMARY KEY CLUSTERED
(
	[ChildId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__GuidLinkChild]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__GUIDLINKCHILD'))
ALTER TABLE [dbo].[__AUDIT__GuidLinkChild] DROP CONSTRAINT [PK___AUDIT__GUIDLINKCHILD]
GO

--PRIMARY KEY FOR TABLE [GuidLinkParent]
if not exists(select * from sysobjects where name = 'PK_GUIDLINKPARENT' and xtype = 'PK')
ALTER TABLE [dbo].[GuidLinkParent] WITH NOCHECK ADD 
CONSTRAINT [PK_GUIDLINKPARENT] PRIMARY KEY CLUSTERED
(
	[Id]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__GuidLinkParent]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__GUIDLINKPARENT'))
ALTER TABLE [dbo].[__AUDIT__GuidLinkParent] DROP CONSTRAINT [PK___AUDIT__GUIDLINKPARENT]
GO

--PRIMARY KEY FOR TABLE [Left]
if not exists(select * from sysobjects where name = 'PK_LEFT' and xtype = 'PK')
ALTER TABLE [dbo].[Left] WITH NOCHECK ADD 
CONSTRAINT [PK_LEFT] PRIMARY KEY CLUSTERED
(
	[LeftId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__Left]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__LEFT'))
ALTER TABLE [dbo].[__AUDIT__Left] DROP CONSTRAINT [PK___AUDIT__LEFT]
GO

--PRIMARY KEY FOR TABLE [LeftRight]
if not exists(select * from sysobjects where name = 'PK_LEFTRIGHT' and xtype = 'PK')
ALTER TABLE [dbo].[LeftRight] WITH NOCHECK ADD 
CONSTRAINT [PK_LEFTRIGHT] PRIMARY KEY CLUSTERED
(
	[FleftId],
	[FrightId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__LeftRight]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__LEFTRIGHT'))
ALTER TABLE [dbo].[__AUDIT__LeftRight] DROP CONSTRAINT [PK___AUDIT__LEFTRIGHT]
GO

--PRIMARY KEY FOR TABLE [ManyTypes]
if not exists(select * from sysobjects where name = 'PK_MANYTYPES' and xtype = 'PK')
ALTER TABLE [dbo].[ManyTypes] WITH NOCHECK ADD 
CONSTRAINT [PK_MANYTYPES] PRIMARY KEY CLUSTERED
(
	[GuidId]
) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [__AUDIT__ManyTypes]
if not exists(select * from sysobjects where name = 'PK___AUDIT__MANYTYPES' and xtype = 'PK')
ALTER TABLE [dbo].[__AUDIT__ManyTypes] WITH NOCHECK ADD
CONSTRAINT [PK___AUDIT__MANYTYPES] PRIMARY KEY CLUSTERED ([__rowid]) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [OneToOneChild]
if not exists(select * from sysobjects where name = 'PK_ONETOONECHILD' and xtype = 'PK')
ALTER TABLE [dbo].[OneToOneChild] WITH NOCHECK ADD 
CONSTRAINT [PK_ONETOONECHILD] PRIMARY KEY CLUSTERED
(
	[UserId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__OneToOneChild]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__ONETOONECHILD'))
ALTER TABLE [dbo].[__AUDIT__OneToOneChild] DROP CONSTRAINT [PK___AUDIT__ONETOONECHILD]
GO

--PRIMARY KEY FOR TABLE [OneToOneParent]
if not exists(select * from sysobjects where name = 'PK_ONETOONEPARENT' and xtype = 'PK')
ALTER TABLE [dbo].[OneToOneParent] WITH NOCHECK ADD 
CONSTRAINT [PK_ONETOONEPARENT] PRIMARY KEY CLUSTERED
(
	[UserId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__OneToOneParent]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__ONETOONEPARENT'))
ALTER TABLE [dbo].[__AUDIT__OneToOneParent] DROP CONSTRAINT [PK___AUDIT__ONETOONEPARENT]
GO

--PRIMARY KEY FOR TABLE [Parent]
if not exists(select * from sysobjects where name = 'PK_PARENT' and xtype = 'PK')
ALTER TABLE [dbo].[Parent] WITH NOCHECK ADD 
CONSTRAINT [PK_PARENT] PRIMARY KEY CLUSTERED
(
	[ParentId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__Parent]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__PARENT'))
ALTER TABLE [dbo].[__AUDIT__Parent] DROP CONSTRAINT [PK___AUDIT__PARENT]
GO

--PRIMARY KEY FOR TABLE [ParentType]
if not exists(select * from sysobjects where name = 'PK_PARENTTYPE' and xtype = 'PK')
ALTER TABLE [dbo].[ParentType] WITH NOCHECK ADD 
CONSTRAINT [PK_PARENTTYPE] PRIMARY KEY CLUSTERED
(
	[ParentTypeId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__ParentType]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__PARENTTYPE'))
ALTER TABLE [dbo].[__AUDIT__ParentType] DROP CONSTRAINT [PK___AUDIT__PARENTTYPE]
GO

--PRIMARY KEY FOR TABLE [Right]
if not exists(select * from sysobjects where name = 'PK_RIGHT' and xtype = 'PK')
ALTER TABLE [dbo].[Right] WITH NOCHECK ADD 
CONSTRAINT [PK_RIGHT] PRIMARY KEY CLUSTERED
(
	[RightId]
) ON [PRIMARY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__Right]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__RIGHT'))
ALTER TABLE [dbo].[__AUDIT__Right] DROP CONSTRAINT [PK___AUDIT__RIGHT]
GO

--PRIMARY KEY FOR TABLE [Simple]
if not exists(select * from sysobjects where name = 'PK_SIMPLE' and xtype = 'PK')
ALTER TABLE [dbo].[Simple] WITH NOCHECK ADD 
CONSTRAINT [PK_SIMPLE] PRIMARY KEY CLUSTERED
(
	[SimpleId]
) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [__AUDIT__Simple]
if not exists(select * from sysobjects where name = 'PK___AUDIT__SIMPLE' and xtype = 'PK')
ALTER TABLE [dbo].[__AUDIT__Simple] WITH NOCHECK ADD
CONSTRAINT [PK___AUDIT__SIMPLE] PRIMARY KEY CLUSTERED ([__rowid]) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [TreeNode]
if not exists(select * from sysobjects where name = 'PK_TREENODE' and xtype = 'PK')
ALTER TABLE [dbo].[TreeNode] WITH NOCHECK ADD 
CONSTRAINT [PK_TREENODE] PRIMARY KEY CLUSTERED
(
	[TreeNodeId]
) ON [PRIMARY]
GO

--PRIMARY KEY FOR TABLE [__AUDIT__TreeNode]
if not exists(select * from sysobjects where name = 'PK___AUDIT__TREENODE' and xtype = 'PK')
ALTER TABLE [dbo].[__AUDIT__TreeNode] WITH NOCHECK ADD
CONSTRAINT [PK___AUDIT__TREENODE] PRIMARY KEY CLUSTERED ([__rowid]) ON [PRIMARY]
GO

--UNIQUE COLUMN TABLE [DoubleLinkOneOneChild].[DoubleLinkParentKey1] (NON-PRIMARY KEY)
if not exists(select * from sys.indexes where name = 'IX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY1')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD CONSTRAINT [IX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY1] UNIQUE ([DoubleLinkParentKey1]) 
GO

--UNIQUE COLUMN TABLE [DoubleLinkOneOneChild].[DoubleLinkParentKey2] (NON-PRIMARY KEY)
if not exists(select * from sys.indexes where name = 'IX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY2')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD CONSTRAINT [IX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY2] UNIQUE ([DoubleLinkParentKey2]) 
GO

--FOREIGN KEY RELATIONSHIP [Parent] -> [Child] ([Parent].[ParentId] -> [Child].[FparentId])
if not exists(select * from sysobjects where name = 'FK_MYROLE_CHILD_PARENT' and xtype = 'F')
ALTER TABLE [dbo].[Child] ADD 
CONSTRAINT [FK_MYROLE_CHILD_PARENT] FOREIGN KEY 
(
	[FparentId]
) REFERENCES [dbo].[Parent] (
	[ParentId]
)
GO

--FOREIGN KEY RELATIONSHIP [DescendantParent] -> [Descendant] ([DescendantParent].[DecendantParentId] -> [Descendant].[FdecendantParentId])
if not exists(select * from sysobjects where name = 'FK__DESCENDANT_DESCENDANTPARENT' and xtype = 'F')
ALTER TABLE [dbo].[Descendant] ADD 
CONSTRAINT [FK__DESCENDANT_DESCENDANTPARENT] FOREIGN KEY 
(
	[FdecendantParentId]
) REFERENCES [dbo].[DescendantParent] (
	[DecendantParentId]
)
GO

--FOREIGN KEY RELATIONSHIP [Ancestor] -> [Descendant] ([Ancestor].[AncestorId] -> [Descendant].[AncestorId])
if not exists(select * from sysobjects where name = 'FK__DESCENDANT_ANCESTOR' and xtype = 'F')
ALTER TABLE [dbo].[Descendant] ADD 
CONSTRAINT [FK__DESCENDANT_ANCESTOR] FOREIGN KEY 
(
	[AncestorId]
) REFERENCES [dbo].[Ancestor] (
	[AncestorId]
)
GO

--FOREIGN KEY RELATIONSHIP [DoubleLinkOneOneParent] -> [DoubleLinkOneOneChild] ([DoubleLinkOneOneParent].[DoubleLinkParentKey1] -> [DoubleLinkOneOneChild].[DoubleLinkParentKey1], [DoubleLinkOneOneParent].[DoubleLinkParentKey2] -> [DoubleLinkOneOneChild].[DoubleLinkParentKey2])
if not exists(select * from sysobjects where name = 'FK__DOUBLELINKONEONECHILD_DOUBLELINKONEONEPARENT' and xtype = 'F')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ADD 
CONSTRAINT [FK__DOUBLELINKONEONECHILD_DOUBLELINKONEONEPARENT] FOREIGN KEY 
(
	[DoubleLinkParentKey1],
	[DoubleLinkParentKey2]
) REFERENCES [dbo].[DoubleLinkOneOneParent] (
	[DoubleLinkParentKey1],
	[DoubleLinkParentKey2]
)
GO

--FOREIGN KEY RELATIONSHIP [GuidLinkParent] -> [GuidLinkChild] ([GuidLinkParent].[Id] -> [GuidLinkChild].[ParentId])
if not exists(select * from sysobjects where name = 'FK__GUIDLINKCHILD_GUIDLINKPARENT' and xtype = 'F')
ALTER TABLE [dbo].[GuidLinkChild] ADD 
CONSTRAINT [FK__GUIDLINKCHILD_GUIDLINKPARENT] FOREIGN KEY 
(
	[ParentId]
) REFERENCES [dbo].[GuidLinkParent] (
	[Id]
)
GO

--FOREIGN KEY RELATIONSHIP [Left] -> [LeftRight] ([Left].[LeftId] -> [LeftRight].[FleftId])
if not exists(select * from sysobjects where name = 'FK__LEFTRIGHT_LEFT' and xtype = 'F')
ALTER TABLE [dbo].[LeftRight] ADD 
CONSTRAINT [FK__LEFTRIGHT_LEFT] FOREIGN KEY 
(
	[FleftId]
) REFERENCES [dbo].[Left] (
	[LeftId]
)
GO

--FOREIGN KEY RELATIONSHIP [Right] -> [LeftRight] ([Right].[RightId] -> [LeftRight].[FrightId])
if not exists(select * from sysobjects where name = 'FK__LEFTRIGHT_RIGHT' and xtype = 'F')
ALTER TABLE [dbo].[LeftRight] ADD 
CONSTRAINT [FK__LEFTRIGHT_RIGHT] FOREIGN KEY 
(
	[FrightId]
) REFERENCES [dbo].[Right] (
	[RightId]
)
GO

--FOREIGN KEY RELATIONSHIP [OneToOneParent] -> [OneToOneChild] ([OneToOneParent].[UserId] -> [OneToOneChild].[UserId])
if not exists(select * from sysobjects where name = 'FK__ONETOONECHILD_ONETOONEPARENT' and xtype = 'F')
ALTER TABLE [dbo].[OneToOneChild] ADD 
CONSTRAINT [FK__ONETOONECHILD_ONETOONEPARENT] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[OneToOneParent] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [ParentType] -> [Parent] ([ParentType].[ParentTypeId] -> [Parent].[FparentTypeId])
if not exists(select * from sysobjects where name = 'FK__PARENT_PARENTTYPE' and xtype = 'F')
ALTER TABLE [dbo].[Parent] ADD 
CONSTRAINT [FK__PARENT_PARENTTYPE] FOREIGN KEY 
(
	[FparentTypeId]
) REFERENCES [dbo].[ParentType] (
	[ParentTypeId]
)
GO

--FOREIGN KEY RELATIONSHIP [TreeNode] -> [TreeNode] ([TreeNode].[TreeNodeId] -> [TreeNode].[FparentTreeNodeId])
if not exists(select * from sysobjects where name = 'FK_PARENT_TREENODE_TREENODE' and xtype = 'F')
ALTER TABLE [dbo].[TreeNode] ADD 
CONSTRAINT [FK_PARENT_TREENODE_TREENODE] FOREIGN KEY 
(
	[FparentTreeNodeId]
) REFERENCES [dbo].[TreeNode] (
	[TreeNodeId]
)
GO

--INDEX FOR TABLE [DoubleLinkOneOneChild] COLUMNS: [DoubleLinkParentKey1]
if not exists(select * from sys.indexes where name = 'IDX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY1')
CREATE INDEX [IDX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY1] ON [dbo].[DoubleLinkOneOneChild] ([DoubleLinkParentKey1] ASC)
 ON [PRIMARY]
GO

--INDEX FOR TABLE [DoubleLinkOneOneChild] COLUMNS: [DoubleLinkParentKey2]
if not exists(select * from sys.indexes where name = 'IDX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY2')
CREATE INDEX [IDX_DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY2] ON [dbo].[DoubleLinkOneOneChild] ([DoubleLinkParentKey2] ASC)
 ON [PRIMARY]
GO

--BEGIN DEFAULTS FOR TABLE [Ancestor]
--DROP UNKNOWN CONSTRAINT FOR '[Ancestor].[AncestorId]' if one exists
declare @Ancestor_AncestorId varchar(500)
set @Ancestor_AncestorId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Ancestor') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Ancestor') and name = 'AncestorId'))
if (@Ancestor_AncestorId IS NOT NULL) exec ('ALTER TABLE [Ancestor] DROP CONSTRAINT [' + @Ancestor_AncestorId + ']')
--DROP KNOWN CONSTRAINT FOR '[Ancestor].[AncestorId]'
if exists(select * from sysobjects where name = 'DF__ANCESTOR_ANCESTORID' and xtype = 'D')
ALTER TABLE [dbo].[Ancestor] DROP CONSTRAINT [DF__ANCESTOR_ANCESTORID] 
--DROP UNKNOWN CONSTRAINT FOR '[Ancestor].[Name]' if one exists
declare @Ancestor_Name varchar(500)
set @Ancestor_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Ancestor') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Ancestor') and name = 'Name'))
if (@Ancestor_Name IS NOT NULL) exec ('ALTER TABLE [Ancestor] DROP CONSTRAINT [' + @Ancestor_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[Ancestor].[Name]'
if exists(select * from sysobjects where name = 'DF__ANCESTOR_NAME' and xtype = 'D')
ALTER TABLE [dbo].[Ancestor] DROP CONSTRAINT [DF__ANCESTOR_NAME] 
--END DEFAULTS FOR TABLE [Ancestor]
GO

--BEGIN DEFAULTS FOR TABLE [Child]
--DROP UNKNOWN CONSTRAINT FOR '[Child].[ChildId]' if one exists
declare @Child_ChildId varchar(500)
set @Child_ChildId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Child') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Child') and name = 'ChildId'))
if (@Child_ChildId IS NOT NULL) exec ('ALTER TABLE [Child] DROP CONSTRAINT [' + @Child_ChildId + ']')
--DROP KNOWN CONSTRAINT FOR '[Child].[ChildId]'
if exists(select * from sysobjects where name = 'DF__CHILD_CHILDID' and xtype = 'D')
ALTER TABLE [dbo].[Child] DROP CONSTRAINT [DF__CHILD_CHILDID] 
--DROP UNKNOWN CONSTRAINT FOR '[Child].[FparentId]' if one exists
declare @Child_FparentId varchar(500)
set @Child_FparentId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Child') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Child') and name = 'FparentId'))
if (@Child_FparentId IS NOT NULL) exec ('ALTER TABLE [Child] DROP CONSTRAINT [' + @Child_FparentId + ']')
--DROP KNOWN CONSTRAINT FOR '[Child].[FparentId]'
if exists(select * from sysobjects where name = 'DF__CHILD_FPARENTID' and xtype = 'D')
ALTER TABLE [dbo].[Child] DROP CONSTRAINT [DF__CHILD_FPARENTID] 
--DROP UNKNOWN CONSTRAINT FOR '[Child].[Name2]' if one exists
declare @Child_name2 varchar(500)
set @Child_name2 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Child') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Child') and name = 'Name2'))
if (@Child_name2 IS NOT NULL) exec ('ALTER TABLE [Child] DROP CONSTRAINT [' + @Child_name2 + ']')
--DROP KNOWN CONSTRAINT FOR '[Child].[Name2]'
if exists(select * from sysobjects where name = 'DF__CHILD_NAME2' and xtype = 'D')
ALTER TABLE [dbo].[Child] DROP CONSTRAINT [DF__CHILD_NAME2] 
--END DEFAULTS FOR TABLE [Child]
GO

--BEGIN DEFAULTS FOR TABLE [Descendant]
--DROP UNKNOWN CONSTRAINT FOR '[Descendant].[AncestorId]' if one exists
declare @Descendant_AncestorId varchar(500)
set @Descendant_AncestorId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and name = 'AncestorId'))
if (@Descendant_AncestorId IS NOT NULL) exec ('ALTER TABLE [Descendant] DROP CONSTRAINT [' + @Descendant_AncestorId + ']')
--DROP KNOWN CONSTRAINT FOR '[Descendant].[AncestorId]'
if exists(select * from sysobjects where name = 'DF__DESCENDANT_ANCESTORID' and xtype = 'D')
ALTER TABLE [dbo].[Descendant] DROP CONSTRAINT [DF__DESCENDANT_ANCESTORID] 
--DROP UNKNOWN CONSTRAINT FOR '[Descendant].[FdecendantParentId]' if one exists
declare @Descendant_FdecendantParentId varchar(500)
set @Descendant_FdecendantParentId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and name = 'FdecendantParentId'))
if (@Descendant_FdecendantParentId IS NOT NULL) exec ('ALTER TABLE [Descendant] DROP CONSTRAINT [' + @Descendant_FdecendantParentId + ']')
--DROP KNOWN CONSTRAINT FOR '[Descendant].[FdecendantParentId]'
if exists(select * from sysobjects where name = 'DF__DESCENDANT_FDECENDANTPARENTID' and xtype = 'D')
ALTER TABLE [dbo].[Descendant] DROP CONSTRAINT [DF__DESCENDANT_FDECENDANTPARENTID] 
--DROP UNKNOWN CONSTRAINT FOR '[Descendant].[LastName]' if one exists
declare @Descendant_LastName varchar(500)
set @Descendant_LastName = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and name = 'LastName'))
if (@Descendant_LastName IS NOT NULL) exec ('ALTER TABLE [Descendant] DROP CONSTRAINT [' + @Descendant_LastName + ']')
--DROP KNOWN CONSTRAINT FOR '[Descendant].[LastName]'
if exists(select * from sysobjects where name = 'DF__DESCENDANT_LASTNAME' and xtype = 'D')
ALTER TABLE [dbo].[Descendant] DROP CONSTRAINT [DF__DESCENDANT_LASTNAME] 
--ADD KNOWN CONSTRAINT FOR '[Descendant].[LastName]'
if not exists(select * from sysobjects where name = 'DF__DESCENDANT_LASTNAME' and xtype = 'D')
ALTER TABLE [dbo].[Descendant] ADD  CONSTRAINT [DF__DESCENDANT_LASTNAME] DEFAULT ('Davis') FOR [LastName]
--DROP UNKNOWN CONSTRAINT FOR '[Descendant].[Price]' if one exists
declare @Descendant_Price varchar(500)
set @Descendant_Price = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Descendant') and name = 'Price'))
if (@Descendant_Price IS NOT NULL) exec ('ALTER TABLE [Descendant] DROP CONSTRAINT [' + @Descendant_Price + ']')
--DROP KNOWN CONSTRAINT FOR '[Descendant].[Price]'
if exists(select * from sysobjects where name = 'DF__DESCENDANT_PRICE' and xtype = 'D')
ALTER TABLE [dbo].[Descendant] DROP CONSTRAINT [DF__DESCENDANT_PRICE] 
--END DEFAULTS FOR TABLE [Descendant]
GO

--BEGIN DEFAULTS FOR TABLE [DescendantParent]
--DROP UNKNOWN CONSTRAINT FOR '[DescendantParent].[DecendantParentId]' if one exists
declare @DescendantParent_DecendantParentId varchar(500)
set @DescendantParent_DecendantParentId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DescendantParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DescendantParent') and name = 'DecendantParentId'))
if (@DescendantParent_DecendantParentId IS NOT NULL) exec ('ALTER TABLE [DescendantParent] DROP CONSTRAINT [' + @DescendantParent_DecendantParentId + ']')
--DROP KNOWN CONSTRAINT FOR '[DescendantParent].[DecendantParentId]'
if exists(select * from sysobjects where name = 'DF__DESCENDANTPARENT_DECENDANTPARENTID' and xtype = 'D')
ALTER TABLE [dbo].[DescendantParent] DROP CONSTRAINT [DF__DESCENDANTPARENT_DECENDANTPARENTID] 
--DROP UNKNOWN CONSTRAINT FOR '[DescendantParent].[Qqq]' if one exists
declare @DescendantParent_Qqq varchar(500)
set @DescendantParent_Qqq = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DescendantParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DescendantParent') and name = 'Qqq'))
if (@DescendantParent_Qqq IS NOT NULL) exec ('ALTER TABLE [DescendantParent] DROP CONSTRAINT [' + @DescendantParent_Qqq + ']')
--DROP KNOWN CONSTRAINT FOR '[DescendantParent].[Qqq]'
if exists(select * from sysobjects where name = 'DF__DESCENDANTPARENT_QQQ' and xtype = 'D')
ALTER TABLE [dbo].[DescendantParent] DROP CONSTRAINT [DF__DESCENDANTPARENT_QQQ] 
--END DEFAULTS FOR TABLE [DescendantParent]
GO

--BEGIN DEFAULTS FOR TABLE [DoubleKeyIdentity]
--DROP UNKNOWN CONSTRAINT FOR '[DoubleKeyIdentity].[Key]' if one exists
declare @DoubleKeyIdentity_Key varchar(500)
set @DoubleKeyIdentity_Key = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleKeyIdentity') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleKeyIdentity') and name = 'Key'))
if (@DoubleKeyIdentity_Key IS NOT NULL) exec ('ALTER TABLE [DoubleKeyIdentity] DROP CONSTRAINT [' + @DoubleKeyIdentity_Key + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleKeyIdentity].[Key]'
if exists(select * from sysobjects where name = 'DF__DOUBLEKEYIDENTITY_KEY' and xtype = 'D')
ALTER TABLE [dbo].[DoubleKeyIdentity] DROP CONSTRAINT [DF__DOUBLEKEYIDENTITY_KEY] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleKeyIdentity].[Name]' if one exists
declare @DoubleKeyIdentity_Name varchar(500)
set @DoubleKeyIdentity_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleKeyIdentity') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleKeyIdentity') and name = 'Name'))
if (@DoubleKeyIdentity_Name IS NOT NULL) exec ('ALTER TABLE [DoubleKeyIdentity] DROP CONSTRAINT [' + @DoubleKeyIdentity_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleKeyIdentity].[Name]'
if exists(select * from sysobjects where name = 'DF__DOUBLEKEYIDENTITY_NAME' and xtype = 'D')
ALTER TABLE [dbo].[DoubleKeyIdentity] DROP CONSTRAINT [DF__DOUBLEKEYIDENTITY_NAME] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleKeyIdentity].[Pk1]' if one exists
declare @DoubleKeyIdentity_Pk1 varchar(500)
set @DoubleKeyIdentity_Pk1 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleKeyIdentity') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleKeyIdentity') and name = 'Pk1'))
if (@DoubleKeyIdentity_Pk1 IS NOT NULL) exec ('ALTER TABLE [DoubleKeyIdentity] DROP CONSTRAINT [' + @DoubleKeyIdentity_Pk1 + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleKeyIdentity].[Pk1]'
if exists(select * from sysobjects where name = 'DF__DOUBLEKEYIDENTITY_PK1' and xtype = 'D')
ALTER TABLE [dbo].[DoubleKeyIdentity] DROP CONSTRAINT [DF__DOUBLEKEYIDENTITY_PK1] 
--END DEFAULTS FOR TABLE [DoubleKeyIdentity]
GO

--BEGIN DEFAULTS FOR TABLE [DoubleLinkOneOneChild]
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkChildKey1]' if one exists
declare @DoubleLinkOneOneChild_DoubleLinkChildKey1 varchar(500)
set @DoubleLinkOneOneChild_DoubleLinkChildKey1 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and name = 'DoubleLinkChildKey1'))
if (@DoubleLinkOneOneChild_DoubleLinkChildKey1 IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneChild] DROP CONSTRAINT [' + @DoubleLinkOneOneChild_DoubleLinkChildKey1 + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkChildKey1]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONECHILD_DOUBLELINKCHILDKEY1' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] DROP CONSTRAINT [DF__DOUBLELINKONEONECHILD_DOUBLELINKCHILDKEY1] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkChildKey2]' if one exists
declare @DoubleLinkOneOneChild_DoubleLinkChildKey2 varchar(500)
set @DoubleLinkOneOneChild_DoubleLinkChildKey2 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and name = 'DoubleLinkChildKey2'))
if (@DoubleLinkOneOneChild_DoubleLinkChildKey2 IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneChild] DROP CONSTRAINT [' + @DoubleLinkOneOneChild_DoubleLinkChildKey2 + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkChildKey2]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONECHILD_DOUBLELINKCHILDKEY2' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] DROP CONSTRAINT [DF__DOUBLELINKONEONECHILD_DOUBLELINKCHILDKEY2] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkParentKey1]' if one exists
declare @DoubleLinkOneOneChild_DoubleLinkParentKey1 varchar(500)
set @DoubleLinkOneOneChild_DoubleLinkParentKey1 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and name = 'DoubleLinkParentKey1'))
if (@DoubleLinkOneOneChild_DoubleLinkParentKey1 IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneChild] DROP CONSTRAINT [' + @DoubleLinkOneOneChild_DoubleLinkParentKey1 + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkParentKey1]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY1' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] DROP CONSTRAINT [DF__DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY1] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkParentKey2]' if one exists
declare @DoubleLinkOneOneChild_DoubleLinkParentKey2 varchar(500)
set @DoubleLinkOneOneChild_DoubleLinkParentKey2 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and name = 'DoubleLinkParentKey2'))
if (@DoubleLinkOneOneChild_DoubleLinkParentKey2 IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneChild] DROP CONSTRAINT [' + @DoubleLinkOneOneChild_DoubleLinkParentKey2 + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[DoubleLinkParentKey2]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY2' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] DROP CONSTRAINT [DF__DOUBLELINKONEONECHILD_DOUBLELINKPARENTKEY2] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[Qqq]' if one exists
declare @DoubleLinkOneOneChild_Qqq varchar(500)
set @DoubleLinkOneOneChild_Qqq = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneChild') and name = 'Qqq'))
if (@DoubleLinkOneOneChild_Qqq IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneChild] DROP CONSTRAINT [' + @DoubleLinkOneOneChild_Qqq + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneChild].[Qqq]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONECHILD_QQQ' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneChild] DROP CONSTRAINT [DF__DOUBLELINKONEONECHILD_QQQ] 
--END DEFAULTS FOR TABLE [DoubleLinkOneOneChild]
GO

--BEGIN DEFAULTS FOR TABLE [DoubleLinkOneOneParent]
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneParent].[DoubleLinkParentKey1]' if one exists
declare @DoubleLinkOneOneParent_DoubleLinkParentKey1 varchar(500)
set @DoubleLinkOneOneParent_DoubleLinkParentKey1 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneParent') and name = 'DoubleLinkParentKey1'))
if (@DoubleLinkOneOneParent_DoubleLinkParentKey1 IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneParent] DROP CONSTRAINT [' + @DoubleLinkOneOneParent_DoubleLinkParentKey1 + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneParent].[DoubleLinkParentKey1]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONEPARENT_DOUBLELINKPARENTKEY1' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] DROP CONSTRAINT [DF__DOUBLELINKONEONEPARENT_DOUBLELINKPARENTKEY1] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneParent].[DoubleLinkParentKey2]' if one exists
declare @DoubleLinkOneOneParent_DoubleLinkParentKey2 varchar(500)
set @DoubleLinkOneOneParent_DoubleLinkParentKey2 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneParent') and name = 'DoubleLinkParentKey2'))
if (@DoubleLinkOneOneParent_DoubleLinkParentKey2 IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneParent] DROP CONSTRAINT [' + @DoubleLinkOneOneParent_DoubleLinkParentKey2 + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneParent].[DoubleLinkParentKey2]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONEPARENT_DOUBLELINKPARENTKEY2' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] DROP CONSTRAINT [DF__DOUBLELINKONEONEPARENT_DOUBLELINKPARENTKEY2] 
--DROP UNKNOWN CONSTRAINT FOR '[DoubleLinkOneOneParent].[Name]' if one exists
declare @DoubleLinkOneOneParent_Name varchar(500)
set @DoubleLinkOneOneParent_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'DoubleLinkOneOneParent') and name = 'Name'))
if (@DoubleLinkOneOneParent_Name IS NOT NULL) exec ('ALTER TABLE [DoubleLinkOneOneParent] DROP CONSTRAINT [' + @DoubleLinkOneOneParent_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[DoubleLinkOneOneParent].[Name]'
if exists(select * from sysobjects where name = 'DF__DOUBLELINKONEONEPARENT_NAME' and xtype = 'D')
ALTER TABLE [dbo].[DoubleLinkOneOneParent] DROP CONSTRAINT [DF__DOUBLELINKONEONEPARENT_NAME] 
--END DEFAULTS FOR TABLE [DoubleLinkOneOneParent]
GO

--BEGIN DEFAULTS FOR TABLE [GuidLinkChild]
--DROP UNKNOWN CONSTRAINT FOR '[GuidLinkChild].[ChildId]' if one exists
declare @GuidLinkChild_ChildId varchar(500)
set @GuidLinkChild_ChildId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkChild') and name = 'ChildId'))
if (@GuidLinkChild_ChildId IS NOT NULL) exec ('ALTER TABLE [GuidLinkChild] DROP CONSTRAINT [' + @GuidLinkChild_ChildId + ']')
--DROP KNOWN CONSTRAINT FOR '[GuidLinkChild].[ChildId]'
if exists(select * from sysobjects where name = 'DF__GUIDLINKCHILD_CHILDID' and xtype = 'D')
ALTER TABLE [dbo].[GuidLinkChild] DROP CONSTRAINT [DF__GUIDLINKCHILD_CHILDID] 
--DROP UNKNOWN CONSTRAINT FOR '[GuidLinkChild].[ParentId]' if one exists
declare @GuidLinkChild_ParentId varchar(500)
set @GuidLinkChild_ParentId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkChild') and name = 'ParentId'))
if (@GuidLinkChild_ParentId IS NOT NULL) exec ('ALTER TABLE [GuidLinkChild] DROP CONSTRAINT [' + @GuidLinkChild_ParentId + ']')
--DROP KNOWN CONSTRAINT FOR '[GuidLinkChild].[ParentId]'
if exists(select * from sysobjects where name = 'DF__GUIDLINKCHILD_PARENTID' and xtype = 'D')
ALTER TABLE [dbo].[GuidLinkChild] DROP CONSTRAINT [DF__GUIDLINKCHILD_PARENTID] 
--END DEFAULTS FOR TABLE [GuidLinkChild]
GO

--BEGIN DEFAULTS FOR TABLE [GuidLinkParent]
--DROP UNKNOWN CONSTRAINT FOR '[GuidLinkParent].[Id]' if one exists
declare @GuidLinkParent_Id varchar(500)
set @GuidLinkParent_Id = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkParent') and name = 'Id'))
if (@GuidLinkParent_Id IS NOT NULL) exec ('ALTER TABLE [GuidLinkParent] DROP CONSTRAINT [' + @GuidLinkParent_Id + ']')
--DROP KNOWN CONSTRAINT FOR '[GuidLinkParent].[Id]'
if exists(select * from sysobjects where name = 'DF__GUIDLINKPARENT_ID' and xtype = 'D')
ALTER TABLE [dbo].[GuidLinkParent] DROP CONSTRAINT [DF__GUIDLINKPARENT_ID] 
--DROP UNKNOWN CONSTRAINT FOR '[GuidLinkParent].[Name]' if one exists
declare @GuidLinkParent_Name varchar(500)
set @GuidLinkParent_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'GuidLinkParent') and name = 'Name'))
if (@GuidLinkParent_Name IS NOT NULL) exec ('ALTER TABLE [GuidLinkParent] DROP CONSTRAINT [' + @GuidLinkParent_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[GuidLinkParent].[Name]'
if exists(select * from sysobjects where name = 'DF__GUIDLINKPARENT_NAME' and xtype = 'D')
ALTER TABLE [dbo].[GuidLinkParent] DROP CONSTRAINT [DF__GUIDLINKPARENT_NAME] 
--END DEFAULTS FOR TABLE [GuidLinkParent]
GO

--BEGIN DEFAULTS FOR TABLE [Left]
--DROP UNKNOWN CONSTRAINT FOR '[Left].[LeftId]' if one exists
declare @Left_LeftId varchar(500)
set @Left_LeftId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Left') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Left') and name = 'LeftId'))
if (@Left_LeftId IS NOT NULL) exec ('ALTER TABLE [Left] DROP CONSTRAINT [' + @Left_LeftId + ']')
--DROP KNOWN CONSTRAINT FOR '[Left].[LeftId]'
if exists(select * from sysobjects where name = 'DF__LEFT_LEFTID' and xtype = 'D')
ALTER TABLE [dbo].[Left] DROP CONSTRAINT [DF__LEFT_LEFTID] 
--DROP UNKNOWN CONSTRAINT FOR '[Left].[Name]' if one exists
declare @Left_Name varchar(500)
set @Left_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Left') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Left') and name = 'Name'))
if (@Left_Name IS NOT NULL) exec ('ALTER TABLE [Left] DROP CONSTRAINT [' + @Left_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[Left].[Name]'
if exists(select * from sysobjects where name = 'DF__LEFT_NAME' and xtype = 'D')
ALTER TABLE [dbo].[Left] DROP CONSTRAINT [DF__LEFT_NAME] 
--END DEFAULTS FOR TABLE [Left]
GO

--BEGIN DEFAULTS FOR TABLE [LeftRight]
--DROP UNKNOWN CONSTRAINT FOR '[LeftRight].[FleftId]' if one exists
declare @LeftRight_FleftId varchar(500)
set @LeftRight_FleftId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'LeftRight') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'LeftRight') and name = 'FleftId'))
if (@LeftRight_FleftId IS NOT NULL) exec ('ALTER TABLE [LeftRight] DROP CONSTRAINT [' + @LeftRight_FleftId + ']')
--DROP KNOWN CONSTRAINT FOR '[LeftRight].[FleftId]'
if exists(select * from sysobjects where name = 'DF__LEFTRIGHT_FLEFTID' and xtype = 'D')
ALTER TABLE [dbo].[LeftRight] DROP CONSTRAINT [DF__LEFTRIGHT_FLEFTID] 
--DROP UNKNOWN CONSTRAINT FOR '[LeftRight].[FrightId]' if one exists
declare @LeftRight_FrightId varchar(500)
set @LeftRight_FrightId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'LeftRight') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'LeftRight') and name = 'FrightId'))
if (@LeftRight_FrightId IS NOT NULL) exec ('ALTER TABLE [LeftRight] DROP CONSTRAINT [' + @LeftRight_FrightId + ']')
--DROP KNOWN CONSTRAINT FOR '[LeftRight].[FrightId]'
if exists(select * from sysobjects where name = 'DF__LEFTRIGHT_FRIGHTID' and xtype = 'D')
ALTER TABLE [dbo].[LeftRight] DROP CONSTRAINT [DF__LEFTRIGHT_FRIGHTID] 
--END DEFAULTS FOR TABLE [LeftRight]
GO

--BEGIN DEFAULTS FOR TABLE [ManyTypes]
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[GuidId]' if one exists
declare @ManyTypes_GuidId varchar(500)
set @ManyTypes_GuidId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'GuidId'))
if (@ManyTypes_GuidId IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_GuidId + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[GuidId]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_GUIDID' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_GUIDID] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestBigint]' if one exists
declare @ManyTypes_TestBigint varchar(500)
set @ManyTypes_TestBigint = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestBigint'))
if (@ManyTypes_TestBigint IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestBigint + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestBigint]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTBIGINT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTBIGINT] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestBinary]' if one exists
declare @ManyTypes_TestBinary varchar(500)
set @ManyTypes_TestBinary = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestBinary'))
if (@ManyTypes_TestBinary IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestBinary + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestBinary]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTBINARY' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTBINARY] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestBit]' if one exists
declare @ManyTypes_TestBit varchar(500)
set @ManyTypes_TestBit = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestBit'))
if (@ManyTypes_TestBit IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestBit + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestBit]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTBIT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTBIT] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[Testcalculated]' if one exists
declare @ManyTypes_Testcalculated varchar(500)
set @ManyTypes_Testcalculated = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'Testcalculated'))
if (@ManyTypes_Testcalculated IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_Testcalculated + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[Testcalculated]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTCALCULATED' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTCALCULATED] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestChar]' if one exists
declare @ManyTypes_TestChar varchar(500)
set @ManyTypes_TestChar = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestChar'))
if (@ManyTypes_TestChar IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestChar + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestChar]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTCHAR' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTCHAR] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestDate]' if one exists
declare @ManyTypes_TestDate varchar(500)
set @ManyTypes_TestDate = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestDate'))
if (@ManyTypes_TestDate IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestDate + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestDate]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTDATE' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTDATE] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestDatetime]' if one exists
declare @ManyTypes_TestDatetime varchar(500)
set @ManyTypes_TestDatetime = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestDatetime'))
if (@ManyTypes_TestDatetime IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestDatetime + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestDatetime]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTDATETIME' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTDATETIME] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestDatetime2]' if one exists
declare @ManyTypes_TestDatetime2 varchar(500)
set @ManyTypes_TestDatetime2 = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestDatetime2'))
if (@ManyTypes_TestDatetime2 IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestDatetime2 + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestDatetime2]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTDATETIME2' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTDATETIME2] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestDatetimeoffset]' if one exists
declare @ManyTypes_TestDatetimeoffset varchar(500)
set @ManyTypes_TestDatetimeoffset = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestDatetimeoffset'))
if (@ManyTypes_TestDatetimeoffset IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestDatetimeoffset + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestDatetimeoffset]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTDATETIMEOFFSET' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTDATETIMEOFFSET] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestDecimal]' if one exists
declare @ManyTypes_TestDecimal varchar(500)
set @ManyTypes_TestDecimal = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestDecimal'))
if (@ManyTypes_TestDecimal IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestDecimal + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestDecimal]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTDECIMAL' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTDECIMAL] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestFloat]' if one exists
declare @ManyTypes_TestFloat varchar(500)
set @ManyTypes_TestFloat = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestFloat'))
if (@ManyTypes_TestFloat IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestFloat + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestFloat]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTFLOAT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTFLOAT] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestImage]' if one exists
declare @ManyTypes_TestImage varchar(500)
set @ManyTypes_TestImage = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestImage'))
if (@ManyTypes_TestImage IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestImage + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestImage]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTIMAGE' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTIMAGE] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestInt]' if one exists
declare @ManyTypes_TestInt varchar(500)
set @ManyTypes_TestInt = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestInt'))
if (@ManyTypes_TestInt IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestInt + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestInt]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTINT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTINT] 
--ADD KNOWN CONSTRAINT FOR '[ManyTypes].[TestInt]'
if not exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTINT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] ADD  CONSTRAINT [DF__MANYTYPES_TESTINT] DEFAULT (278) FOR [TestInt]
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestMoney]' if one exists
declare @ManyTypes_TestMoney varchar(500)
set @ManyTypes_TestMoney = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestMoney'))
if (@ManyTypes_TestMoney IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestMoney + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestMoney]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTMONEY' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTMONEY] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestNchar]' if one exists
declare @ManyTypes_TestNchar varchar(500)
set @ManyTypes_TestNchar = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestNchar'))
if (@ManyTypes_TestNchar IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestNchar + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestNchar]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTNCHAR' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTNCHAR] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestNtext]' if one exists
declare @ManyTypes_TestNtext varchar(500)
set @ManyTypes_TestNtext = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestNtext'))
if (@ManyTypes_TestNtext IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestNtext + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestNtext]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTNTEXT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTNTEXT] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestNvarchar]' if one exists
declare @ManyTypes_TestNvarchar varchar(500)
set @ManyTypes_TestNvarchar = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestNvarchar'))
if (@ManyTypes_TestNvarchar IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestNvarchar + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestNvarchar]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTNVARCHAR' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTNVARCHAR] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestReal]' if one exists
declare @ManyTypes_TestReal varchar(500)
set @ManyTypes_TestReal = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestReal'))
if (@ManyTypes_TestReal IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestReal + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestReal]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTREAL' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTREAL] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestSmalldatetime]' if one exists
declare @ManyTypes_TestSmalldatetime varchar(500)
set @ManyTypes_TestSmalldatetime = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestSmalldatetime'))
if (@ManyTypes_TestSmalldatetime IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestSmalldatetime + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestSmalldatetime]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTSMALLDATETIME' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTSMALLDATETIME] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestSmallint]' if one exists
declare @ManyTypes_TestSmallint varchar(500)
set @ManyTypes_TestSmallint = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestSmallint'))
if (@ManyTypes_TestSmallint IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestSmallint + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestSmallint]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTSMALLINT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTSMALLINT] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestSmallmoney]' if one exists
declare @ManyTypes_TestSmallmoney varchar(500)
set @ManyTypes_TestSmallmoney = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestSmallmoney'))
if (@ManyTypes_TestSmallmoney IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestSmallmoney + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestSmallmoney]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTSMALLMONEY' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTSMALLMONEY] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestText]' if one exists
declare @ManyTypes_TestText varchar(500)
set @ManyTypes_TestText = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestText'))
if (@ManyTypes_TestText IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestText + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestText]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTTEXT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTTEXT] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestTime]' if one exists
declare @ManyTypes_TestTime varchar(500)
set @ManyTypes_TestTime = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestTime'))
if (@ManyTypes_TestTime IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestTime + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestTime]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTTIME' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTTIME] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestTinyint]' if one exists
declare @ManyTypes_TestTinyint varchar(500)
set @ManyTypes_TestTinyint = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestTinyint'))
if (@ManyTypes_TestTinyint IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestTinyint + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestTinyint]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTTINYINT' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTTINYINT] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestUniqueidentifier]' if one exists
declare @ManyTypes_TestUniqueidentifier varchar(500)
set @ManyTypes_TestUniqueidentifier = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestUniqueidentifier'))
if (@ManyTypes_TestUniqueidentifier IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestUniqueidentifier + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestUniqueidentifier]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTUNIQUEIDENTIFIER' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTUNIQUEIDENTIFIER] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestVarbinary]' if one exists
declare @ManyTypes_TestVarbinary varchar(500)
set @ManyTypes_TestVarbinary = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestVarbinary'))
if (@ManyTypes_TestVarbinary IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestVarbinary + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestVarbinary]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTVARBINARY' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTVARBINARY] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestVarchar]' if one exists
declare @ManyTypes_TestVarchar varchar(500)
set @ManyTypes_TestVarchar = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestVarchar'))
if (@ManyTypes_TestVarchar IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestVarchar + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestVarchar]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTVARCHAR' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTVARCHAR] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[TestXml]' if one exists
declare @ManyTypes_TestXml varchar(500)
set @ManyTypes_TestXml = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'TestXml'))
if (@ManyTypes_TestXml IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_TestXml + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[TestXml]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_TESTXML' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_TESTXML] 
--DROP UNKNOWN CONSTRAINT FOR '[ManyTypes].[Varcharmax]' if one exists
declare @ManyTypes_Varcharmax varchar(500)
set @ManyTypes_Varcharmax = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ManyTypes') and name = 'Varcharmax'))
if (@ManyTypes_Varcharmax IS NOT NULL) exec ('ALTER TABLE [ManyTypes] DROP CONSTRAINT [' + @ManyTypes_Varcharmax + ']')
--DROP KNOWN CONSTRAINT FOR '[ManyTypes].[Varcharmax]'
if exists(select * from sysobjects where name = 'DF__MANYTYPES_VARCHARMAX' and xtype = 'D')
ALTER TABLE [dbo].[ManyTypes] DROP CONSTRAINT [DF__MANYTYPES_VARCHARMAX] 
--END DEFAULTS FOR TABLE [ManyTypes]
GO

--BEGIN DEFAULTS FOR TABLE [OneToOneChild]
--DROP UNKNOWN CONSTRAINT FOR '[OneToOneChild].[LastName]' if one exists
declare @OneToOneChild_LastName varchar(500)
set @OneToOneChild_LastName = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'OneToOneChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'OneToOneChild') and name = 'LastName'))
if (@OneToOneChild_LastName IS NOT NULL) exec ('ALTER TABLE [OneToOneChild] DROP CONSTRAINT [' + @OneToOneChild_LastName + ']')
--DROP KNOWN CONSTRAINT FOR '[OneToOneChild].[LastName]'
if exists(select * from sysobjects where name = 'DF__ONETOONECHILD_LASTNAME' and xtype = 'D')
ALTER TABLE [dbo].[OneToOneChild] DROP CONSTRAINT [DF__ONETOONECHILD_LASTNAME] 
--DROP UNKNOWN CONSTRAINT FOR '[OneToOneChild].[UserId]' if one exists
declare @OneToOneChild_UserId varchar(500)
set @OneToOneChild_UserId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'OneToOneChild') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'OneToOneChild') and name = 'UserId'))
if (@OneToOneChild_UserId IS NOT NULL) exec ('ALTER TABLE [OneToOneChild] DROP CONSTRAINT [' + @OneToOneChild_UserId + ']')
--DROP KNOWN CONSTRAINT FOR '[OneToOneChild].[UserId]'
if exists(select * from sysobjects where name = 'DF__ONETOONECHILD_USERID' and xtype = 'D')
ALTER TABLE [dbo].[OneToOneChild] DROP CONSTRAINT [DF__ONETOONECHILD_USERID] 
--END DEFAULTS FOR TABLE [OneToOneChild]
GO

--BEGIN DEFAULTS FOR TABLE [OneToOneParent]
--DROP UNKNOWN CONSTRAINT FOR '[OneToOneParent].[FirstName]' if one exists
declare @OneToOneParent_FirstName varchar(500)
set @OneToOneParent_FirstName = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'OneToOneParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'OneToOneParent') and name = 'FirstName'))
if (@OneToOneParent_FirstName IS NOT NULL) exec ('ALTER TABLE [OneToOneParent] DROP CONSTRAINT [' + @OneToOneParent_FirstName + ']')
--DROP KNOWN CONSTRAINT FOR '[OneToOneParent].[FirstName]'
if exists(select * from sysobjects where name = 'DF__ONETOONEPARENT_FIRSTNAME' and xtype = 'D')
ALTER TABLE [dbo].[OneToOneParent] DROP CONSTRAINT [DF__ONETOONEPARENT_FIRSTNAME] 
--DROP UNKNOWN CONSTRAINT FOR '[OneToOneParent].[UserId]' if one exists
declare @OneToOneParent_UserId varchar(500)
set @OneToOneParent_UserId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'OneToOneParent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'OneToOneParent') and name = 'UserId'))
if (@OneToOneParent_UserId IS NOT NULL) exec ('ALTER TABLE [OneToOneParent] DROP CONSTRAINT [' + @OneToOneParent_UserId + ']')
--DROP KNOWN CONSTRAINT FOR '[OneToOneParent].[UserId]'
if exists(select * from sysobjects where name = 'DF__ONETOONEPARENT_USERID' and xtype = 'D')
ALTER TABLE [dbo].[OneToOneParent] DROP CONSTRAINT [DF__ONETOONEPARENT_USERID] 
--END DEFAULTS FOR TABLE [OneToOneParent]
GO

--BEGIN DEFAULTS FOR TABLE [Parent]
--DROP UNKNOWN CONSTRAINT FOR '[Parent].[FparentTypeId]' if one exists
declare @Parent_FparentTypeId varchar(500)
set @Parent_FparentTypeId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Parent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Parent') and name = 'FparentTypeId'))
if (@Parent_FparentTypeId IS NOT NULL) exec ('ALTER TABLE [Parent] DROP CONSTRAINT [' + @Parent_FparentTypeId + ']')
--DROP KNOWN CONSTRAINT FOR '[Parent].[FparentTypeId]'
if exists(select * from sysobjects where name = 'DF__PARENT_FPARENTTYPEID' and xtype = 'D')
ALTER TABLE [dbo].[Parent] DROP CONSTRAINT [DF__PARENT_FPARENTTYPEID] 
--DROP UNKNOWN CONSTRAINT FOR '[Parent].[Name]' if one exists
declare @Parent_Name varchar(500)
set @Parent_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Parent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Parent') and name = 'Name'))
if (@Parent_Name IS NOT NULL) exec ('ALTER TABLE [Parent] DROP CONSTRAINT [' + @Parent_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[Parent].[Name]'
if exists(select * from sysobjects where name = 'DF__PARENT_NAME' and xtype = 'D')
ALTER TABLE [dbo].[Parent] DROP CONSTRAINT [DF__PARENT_NAME] 
--DROP UNKNOWN CONSTRAINT FOR '[Parent].[ParentId]' if one exists
declare @Parent_ParentId varchar(500)
set @Parent_ParentId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Parent') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Parent') and name = 'ParentId'))
if (@Parent_ParentId IS NOT NULL) exec ('ALTER TABLE [Parent] DROP CONSTRAINT [' + @Parent_ParentId + ']')
--DROP KNOWN CONSTRAINT FOR '[Parent].[ParentId]'
if exists(select * from sysobjects where name = 'DF__PARENT_PARENTID' and xtype = 'D')
ALTER TABLE [dbo].[Parent] DROP CONSTRAINT [DF__PARENT_PARENTID] 
--END DEFAULTS FOR TABLE [Parent]
GO

--BEGIN DEFAULTS FOR TABLE [ParentType]
--DROP UNKNOWN CONSTRAINT FOR '[ParentType].[Name]' if one exists
declare @ParentType_Name varchar(500)
set @ParentType_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ParentType') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ParentType') and name = 'Name'))
if (@ParentType_Name IS NOT NULL) exec ('ALTER TABLE [ParentType] DROP CONSTRAINT [' + @ParentType_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[ParentType].[Name]'
if exists(select * from sysobjects where name = 'DF__PARENTTYPE_NAME' and xtype = 'D')
ALTER TABLE [dbo].[ParentType] DROP CONSTRAINT [DF__PARENTTYPE_NAME] 
--DROP UNKNOWN CONSTRAINT FOR '[ParentType].[ParentTypeId]' if one exists
declare @ParentType_ParentTypeId varchar(500)
set @ParentType_ParentTypeId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'ParentType') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'ParentType') and name = 'ParentTypeId'))
if (@ParentType_ParentTypeId IS NOT NULL) exec ('ALTER TABLE [ParentType] DROP CONSTRAINT [' + @ParentType_ParentTypeId + ']')
--DROP KNOWN CONSTRAINT FOR '[ParentType].[ParentTypeId]'
if exists(select * from sysobjects where name = 'DF__PARENTTYPE_PARENTTYPEID' and xtype = 'D')
ALTER TABLE [dbo].[ParentType] DROP CONSTRAINT [DF__PARENTTYPE_PARENTTYPEID] 
--END DEFAULTS FOR TABLE [ParentType]
GO

--BEGIN DEFAULTS FOR TABLE [Right]
--DROP UNKNOWN CONSTRAINT FOR '[Right].[Name]' if one exists
declare @Right_Name varchar(500)
set @Right_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Right') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Right') and name = 'Name'))
if (@Right_Name IS NOT NULL) exec ('ALTER TABLE [Right] DROP CONSTRAINT [' + @Right_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[Right].[Name]'
if exists(select * from sysobjects where name = 'DF__RIGHT_NAME' and xtype = 'D')
ALTER TABLE [dbo].[Right] DROP CONSTRAINT [DF__RIGHT_NAME] 
--DROP UNKNOWN CONSTRAINT FOR '[Right].[RightId]' if one exists
declare @Right_RightId varchar(500)
set @Right_RightId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Right') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Right') and name = 'RightId'))
if (@Right_RightId IS NOT NULL) exec ('ALTER TABLE [Right] DROP CONSTRAINT [' + @Right_RightId + ']')
--DROP KNOWN CONSTRAINT FOR '[Right].[RightId]'
if exists(select * from sysobjects where name = 'DF__RIGHT_RIGHTID' and xtype = 'D')
ALTER TABLE [dbo].[Right] DROP CONSTRAINT [DF__RIGHT_RIGHTID] 
--END DEFAULTS FOR TABLE [Right]
GO

--BEGIN DEFAULTS FOR TABLE [Simple]
--DROP UNKNOWN CONSTRAINT FOR '[Simple].[MyReadonly]' if one exists
declare @Simple_MyReadonly varchar(500)
set @Simple_MyReadonly = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Simple') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Simple') and name = 'MyReadonly'))
if (@Simple_MyReadonly IS NOT NULL) exec ('ALTER TABLE [Simple] DROP CONSTRAINT [' + @Simple_MyReadonly + ']')
--DROP KNOWN CONSTRAINT FOR '[Simple].[MyReadonly]'
if exists(select * from sysobjects where name = 'DF__SIMPLE_MYREADONLY' and xtype = 'D')
ALTER TABLE [dbo].[Simple] DROP CONSTRAINT [DF__SIMPLE_MYREADONLY] 
--ADD KNOWN CONSTRAINT FOR '[Simple].[MyReadonly]'
if not exists(select * from sysobjects where name = 'DF__SIMPLE_MYREADONLY' and xtype = 'D')
ALTER TABLE [dbo].[Simple] ADD  CONSTRAINT [DF__SIMPLE_MYREADONLY] DEFAULT (0) FOR [MyReadonly]
--DROP UNKNOWN CONSTRAINT FOR '[Simple].[Name]' if one exists
declare @Simple_Name varchar(500)
set @Simple_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Simple') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Simple') and name = 'Name'))
if (@Simple_Name IS NOT NULL) exec ('ALTER TABLE [Simple] DROP CONSTRAINT [' + @Simple_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[Simple].[Name]'
if exists(select * from sysobjects where name = 'DF__SIMPLE_NAME' and xtype = 'D')
ALTER TABLE [dbo].[Simple] DROP CONSTRAINT [DF__SIMPLE_NAME] 
--DROP UNKNOWN CONSTRAINT FOR '[Simple].[NewInt]' if one exists
declare @Simple_NewInt varchar(500)
set @Simple_NewInt = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Simple') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Simple') and name = 'NewInt'))
if (@Simple_NewInt IS NOT NULL) exec ('ALTER TABLE [Simple] DROP CONSTRAINT [' + @Simple_NewInt + ']')
--DROP KNOWN CONSTRAINT FOR '[Simple].[NewInt]'
if exists(select * from sysobjects where name = 'DF__SIMPLE_NEWINT' and xtype = 'D')
ALTER TABLE [dbo].[Simple] DROP CONSTRAINT [DF__SIMPLE_NEWINT] 
--ADD KNOWN CONSTRAINT FOR '[Simple].[NewInt]'
if not exists(select * from sysobjects where name = 'DF__SIMPLE_NEWINT' and xtype = 'D')
ALTER TABLE [dbo].[Simple] ADD  CONSTRAINT [DF__SIMPLE_NEWINT] DEFAULT (0) FOR [NewInt]
--DROP UNKNOWN CONSTRAINT FOR '[Simple].[SimpleId]' if one exists
declare @Simple_SimpleId varchar(500)
set @Simple_SimpleId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'Simple') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'Simple') and name = 'SimpleId'))
if (@Simple_SimpleId IS NOT NULL) exec ('ALTER TABLE [Simple] DROP CONSTRAINT [' + @Simple_SimpleId + ']')
--DROP KNOWN CONSTRAINT FOR '[Simple].[SimpleId]'
if exists(select * from sysobjects where name = 'DF__SIMPLE_SIMPLEID' and xtype = 'D')
ALTER TABLE [dbo].[Simple] DROP CONSTRAINT [DF__SIMPLE_SIMPLEID] 
--END DEFAULTS FOR TABLE [Simple]
GO

--BEGIN DEFAULTS FOR TABLE [TreeNode]
--DROP UNKNOWN CONSTRAINT FOR '[TreeNode].[FparentTreeNodeId]' if one exists
declare @TreeNode_FparentTreeNodeId varchar(500)
set @TreeNode_FparentTreeNodeId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'TreeNode') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'TreeNode') and name = 'FparentTreeNodeId'))
if (@TreeNode_FparentTreeNodeId IS NOT NULL) exec ('ALTER TABLE [TreeNode] DROP CONSTRAINT [' + @TreeNode_FparentTreeNodeId + ']')
--DROP KNOWN CONSTRAINT FOR '[TreeNode].[FparentTreeNodeId]'
if exists(select * from sysobjects where name = 'DF__TREENODE_FPARENTTREENODEID' and xtype = 'D')
ALTER TABLE [dbo].[TreeNode] DROP CONSTRAINT [DF__TREENODE_FPARENTTREENODEID] 
--DROP UNKNOWN CONSTRAINT FOR '[TreeNode].[Name]' if one exists
declare @TreeNode_Name varchar(500)
set @TreeNode_Name = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'TreeNode') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'TreeNode') and name = 'Name'))
if (@TreeNode_Name IS NOT NULL) exec ('ALTER TABLE [TreeNode] DROP CONSTRAINT [' + @TreeNode_Name + ']')
--DROP KNOWN CONSTRAINT FOR '[TreeNode].[Name]'
if exists(select * from sysobjects where name = 'DF__TREENODE_NAME' and xtype = 'D')
ALTER TABLE [dbo].[TreeNode] DROP CONSTRAINT [DF__TREENODE_NAME] 
--DROP UNKNOWN CONSTRAINT FOR '[TreeNode].[TreeNodeId]' if one exists
declare @TreeNode_TreeNodeId varchar(500)
set @TreeNode_TreeNodeId = (SELECT top 1 name FROM sys.default_constraints WHERE parent_object_id = (SELECT OBJECT_ID FROM sys.tables WHERE name = 'TreeNode') and parent_column_id = (select top 1 column_id from sys.columns where object_id = (SELECT top 1 OBJECT_ID FROM sys.tables WHERE name = 'TreeNode') and name = 'TreeNodeId'))
if (@TreeNode_TreeNodeId IS NOT NULL) exec ('ALTER TABLE [TreeNode] DROP CONSTRAINT [' + @TreeNode_TreeNodeId + ']')
--DROP KNOWN CONSTRAINT FOR '[TreeNode].[TreeNodeId]'
if exists(select * from sysobjects where name = 'DF__TREENODE_TREENODEID' and xtype = 'D')
ALTER TABLE [dbo].[TreeNode] DROP CONSTRAINT [DF__TREENODE_TREENODEID] 
--END DEFAULTS FOR TABLE [TreeNode]
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [Ancestor]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Ancestor' and c.name = 'AncestorId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Ancestor] ALTER COLUMN [AncestorId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Ancestor' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Ancestor] ALTER COLUMN [Name] [VarChar] (50) NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [Child]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Child' and c.name = 'ChildId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Child] ALTER COLUMN [ChildId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Child' and c.name = 'FparentId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Child] ALTER COLUMN [FparentId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Child' and c.name = 'Name2' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Child] ALTER COLUMN [Name2] [VarChar] (100) NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [Descendant]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Descendant' and c.name = 'AncestorId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Descendant] ALTER COLUMN [AncestorId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Descendant' and c.name = 'FdecendantParentId' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[Descendant] ALTER COLUMN [FdecendantParentId] [Int] NULL
END
if not exists(select * from sys.objects where type_desc = 'DEFAULT_CONSTRAINT' and name = 'DF__DESCENDANT_LASTNAME')
ALTER TABLE [dbo].[Descendant] ADD CONSTRAINT [DF__DESCENDANT_LASTNAME] DEFAULT ('Davis') FOR [LastName]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Descendant' and c.name = 'LastName' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Descendant] ALTER COLUMN [LastName] [VarChar] (50) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Descendant' and c.name = 'Price' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Descendant] ALTER COLUMN [Price] [Decimal] (20, 0) NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [DescendantParent]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DescendantParent' and c.name = 'DecendantParentId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DescendantParent] ALTER COLUMN [DecendantParentId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DescendantParent' and c.name = 'Qqq' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[DescendantParent] ALTER COLUMN [Qqq] [VarChar] (50) NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [DoubleKeyIdentity]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleKeyIdentity' and c.name = 'Key' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleKeyIdentity] ALTER COLUMN [Key] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleKeyIdentity' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[DoubleKeyIdentity] ALTER COLUMN [Name] [VarChar] (50) NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleKeyIdentity' and c.name = 'Pk1' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleKeyIdentity] ALTER COLUMN [Pk1] [VarChar] (50) NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [DoubleLinkOneOneChild]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneChild' and c.name = 'DoubleLinkChildKey1' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ALTER COLUMN [DoubleLinkChildKey1] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneChild' and c.name = 'DoubleLinkChildKey2' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ALTER COLUMN [DoubleLinkChildKey2] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneChild' and c.name = 'DoubleLinkParentKey1' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ALTER COLUMN [DoubleLinkParentKey1] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneChild' and c.name = 'DoubleLinkParentKey2' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ALTER COLUMN [DoubleLinkParentKey2] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneChild' and c.name = 'Qqq' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneChild] ALTER COLUMN [Qqq] [VarChar] (50) NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [DoubleLinkOneOneParent]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneParent' and c.name = 'DoubleLinkParentKey1' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ALTER COLUMN [DoubleLinkParentKey1] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneParent' and c.name = 'DoubleLinkParentKey2' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ALTER COLUMN [DoubleLinkParentKey2] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'DoubleLinkOneOneParent' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[DoubleLinkOneOneParent] ALTER COLUMN [Name] [VarChar] (50) NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [GuidLinkChild]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'GuidLinkChild' and c.name = 'ChildId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[GuidLinkChild] ALTER COLUMN [ChildId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'GuidLinkChild' and c.name = 'ParentId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[GuidLinkChild] ALTER COLUMN [ParentId] [UniqueIdentifier] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [GuidLinkParent]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'GuidLinkParent' and c.name = 'Id' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[GuidLinkParent] ALTER COLUMN [Id] [UniqueIdentifier] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'GuidLinkParent' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[GuidLinkParent] ALTER COLUMN [Name] [VarChar] (50) NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [Left]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Left' and c.name = 'LeftId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Left] ALTER COLUMN [LeftId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Left' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Left] ALTER COLUMN [Name] [VarChar] (50) NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [LeftRight]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'LeftRight' and c.name = 'FleftId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[LeftRight] ALTER COLUMN [FleftId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'LeftRight' and c.name = 'FrightId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[LeftRight] ALTER COLUMN [FrightId] [Int] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [ManyTypes]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'GuidId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [GuidId] [UniqueIdentifier] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestBigint' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestBigint] [BigInt] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestBinary' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestBinary] [Binary] (100) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestBit' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestBit] [Bit] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestChar' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestChar] [Char] (100) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestDate' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestDate] [Date] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestDatetime' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestDatetime] [DateTime] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestDatetime2' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestDatetime2] [DateTime2] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestDatetimeoffset' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestDatetimeoffset] [DateTimeOffset] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestDecimal' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestDecimal] [Decimal] (20, 2) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestFloat' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestFloat] [Float] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestImage' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestImage] [Image] NOT NULL
END
if not exists(select * from sys.objects where type_desc = 'DEFAULT_CONSTRAINT' and name = 'DF__MANYTYPES_TESTINT')
ALTER TABLE [dbo].[ManyTypes] ADD CONSTRAINT [DF__MANYTYPES_TESTINT] DEFAULT (278) FOR [TestInt]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestInt' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestInt] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestMoney' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestMoney] [Money] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestNchar' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestNchar] [NChar] (100) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestNtext' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestNtext] [NText] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestNvarchar' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestNvarchar] [NVarChar] (100) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestReal' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestReal] [Real] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestSmalldatetime' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestSmalldatetime] [SmallDateTime] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestSmallint' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestSmallint] [SmallInt] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestSmallmoney' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestSmallmoney] [SmallMoney] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestText' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestText] [Text] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestTime' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestTime] [Time] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestTinyint' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestTinyint] [TinyInt] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestUniqueidentifier' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestUniqueidentifier] [UniqueIdentifier] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestVarbinary' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestVarbinary] [VarBinary] (100) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestVarchar' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestVarchar] [VarChar] (100) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'TestXml' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [TestXml] [Xml] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ManyTypes' and c.name = 'Varcharmax' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ManyTypes] ALTER COLUMN [Varcharmax] [VarChar] (max) NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [OneToOneChild]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'OneToOneChild' and c.name = 'LastName' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[OneToOneChild] ALTER COLUMN [LastName] [VarChar] (50) NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'OneToOneChild' and c.name = 'UserId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[OneToOneChild] ALTER COLUMN [UserId] [Int] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [OneToOneParent]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'OneToOneParent' and c.name = 'FirstName' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[OneToOneParent] ALTER COLUMN [FirstName] [VarChar] (50) NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'OneToOneParent' and c.name = 'UserId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[OneToOneParent] ALTER COLUMN [UserId] [Int] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [Parent]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Parent' and c.name = 'FparentTypeId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Parent] ALTER COLUMN [FparentTypeId] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Parent' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Parent] ALTER COLUMN [Name] [VarChar] (100) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Parent' and c.name = 'ParentId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Parent] ALTER COLUMN [ParentId] [Int] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [ParentType]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ParentType' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ParentType] ALTER COLUMN [Name] [VarChar] (50) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'ParentType' and c.name = 'ParentTypeId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[ParentType] ALTER COLUMN [ParentTypeId] [Int] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [Right]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Right' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Right] ALTER COLUMN [Name] [VarChar] (50) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Right' and c.name = 'RightId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Right] ALTER COLUMN [RightId] [Int] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [Simple]
if not exists(select * from sys.objects where type_desc = 'DEFAULT_CONSTRAINT' and name = 'DF__SIMPLE_MYREADONLY')
ALTER TABLE [dbo].[Simple] ADD CONSTRAINT [DF__SIMPLE_MYREADONLY] DEFAULT (0) FOR [MyReadonly]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Simple' and c.name = 'MyReadonly' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Simple] ALTER COLUMN [MyReadonly] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Simple' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[Simple] ALTER COLUMN [Name] [VarChar] (50) NULL
END
if not exists(select * from sys.objects where type_desc = 'DEFAULT_CONSTRAINT' and name = 'DF__SIMPLE_NEWINT')
ALTER TABLE [dbo].[Simple] ADD CONSTRAINT [DF__SIMPLE_NEWINT] DEFAULT (0) FOR [NewInt]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Simple' and c.name = 'NewInt' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Simple] ALTER COLUMN [NewInt] [Int] NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'Simple' and c.name = 'SimpleId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[Simple] ALTER COLUMN [SimpleId] [Int] NOT NULL
END
GO

--VERIFY COLUMNS ARE CORRECTLY NULLABLE FOR TABLE [TreeNode]
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'TreeNode' and c.name = 'FparentTreeNodeId' and o.type = 'U' and c.is_nullable = 0)
BEGIN
ALTER TABLE [dbo].[TreeNode] ALTER COLUMN [FparentTreeNodeId] [Int] NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'TreeNode' and c.name = 'Name' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[TreeNode] ALTER COLUMN [Name] [VarChar] (50) NOT NULL
END
if exists(select s.name as schemaname, o.name as tablename, c.name as columnname, c.is_nullable from sys.objects o inner join sys.columns c on o.object_id = c.object_id inner join sys.schemas s on o.schema_id = s.schema_id where s.name = 'dbo' AND o.name = 'TreeNode' and c.name = 'TreeNodeId' and o.type = 'U' and c.is_nullable = 1)
BEGIN
ALTER TABLE [dbo].[TreeNode] ALTER COLUMN [TreeNodeId] [Int] NOT NULL
END
GO

